Write JavaScript like a pro
Rip off the coding convention band-aid with this guide on the best JavaScript practices to write JavaScript like a pro
Table of contents
Style guides also known as coding conventions help improve code readability and provide a set of guidelines programmers abide by to make coding maintenance and production easier.
This is extremely important when working in teams or even for your practice. It makes your code easier to upscale, read and allows for easier collaboration.
These rules are not exclusive to JavaScript, you'll find every language has its own set of rules you should follow, and odds are you'll encounter a big chunk of the ones discussed in this article since they're known as best practices.
Declaring variables
Declare variables as local variables using let or const
we don't talk about varUse camelCase for both variables and functions
let articleExample = "pretty nice example"; const secondExample = "another good example";
Make sure to use spaces around operators
Use two spaces for indentation
const stringToCheck = prompt("Enter word to check"); function checkPalindrome(stringToCheck) { const len = stringToCheck.length for(let i = 0; i < len / 2; i++) { if(stringToCheck[i] !== stringToCheck[len - 1 - i]) { return ("It's not a palindrome") } } return ("It's a palindrome") } checkPalindrome(stringToCheck);
Start every name with a letter, avoid numbers, hyphens or symbols
const normalVariableName = ""; const $wrongVariableName = "";
Put all your declarations at the top of each function or file
Make sure to initialize variables as you declare them to avoid undefined errors
let car = "Lexus";
A variable can change the data type from an already set variable. Be careful.
let greeting = "Welcome"; greeting = 21; ///this changes greeting to be a number when it was previously a string
- The opposite can also happen and numbers can be converted into strings giving room to the not-a-number NaN error.
Objects and Arrays
- Declare objects and arrays with const when you want to avoid accidental changes
- Declare objects and arrays with let when you change the values within your code
- Declare new empty values instead of newObjects()
let string = "";
let newArray = [];
let newObject = {};
let price = 0;
let boolean = false;
Open the bracket on the same line you named your object
Use a colon between values and properties
Don't add a comma after the final value
Place the closing bracket in a new line, preferably on the same level as your opening bracket
const doggo = {
dogSize: "Medium",
dogColor: "White",
dogName: "Chloe",
dogPaws: 4
};
- If you have few properties you can write your object in one line killer.
const doggo = { dogSize: "Medium", dogColor: "White", dogPaws: 4 };
Comparison
- To compare use === over ==
- === compares both value and type
- == converts to matching types before comparing values
0 == "0" //true
0 === "0" //false
That's all folks! Do you have any other conventions I missed? Make sure to leave a comment and let me know!
I hope you learned something new today and don't forget to connect with me.
See you on the next one!!
☕If you enjoy my content Buy me a coffee It'll help me continue making quality blogs💕
💙Follow me on Twitter to know more about my self-taught journey!
💜 Web Design for Beginners Series
💜 JavaScript For Newbies Series
❤️ Also subscribe to my Youtube Channel !
🖤And for more content and tips on TikTok !