Do's and don'ts in JavaScript
Today we will provide you the list of the do's and don'ts in JavaScript as per the Google JavaScript Style Guide. When we are working with the real project at that time we have to follow the standard programming guidelines which will help us to write the error free codes.
Do's and don'ts in JavaScript for newbies, JavaScript naming conventions: do's and don'ts, What are the do's and don'ts when coding in JavaScript?, JavaScript Norms.
Checkout more articles on JavaScript
- Replace all occurrences of a string in JavaScript
- Arrow Functions in JavaScript
- Get Current URL in JavaScript
- How to Sort an Array in JavaScript
- Spread Operator in JavaScript
List of the Do's and don'ts in JavaScript
- Use const and let
- Local Variable Declaration
- File Name
- Package Name
- Class Name
- Method Name
- Constant Name
- Parameter Name
1. Use const and let
Declare all local variables with either const or let. Use const by default, unless a variable needs to be reassigned. The var keyword must not be used.
2. Local Variable Declaration
Declare the local variables with either let or const keyword. Use const by default if the variable doesn’t need to be reassigned.
Local variable names are written in lowerCamelCase.
One variable per declaration. (For example: `let a = 1 , b = 2` // It’s not good practice.)
3. File Name
File names must be all lowercase and may include underscores ( `_` ) or dashes ( `-` ), but no additional punctuation. Follow the convention that your project uses.
4. Package Name
Package names are all lowerCamelCase. (For example, `my.testExample.deepSpace`, but not `my.testexample.deepspace` or `my.test_example.deep_space`)
5. Class Name
Class names, interface names, typedef, and record names are all UpperCamelCase.
6. Method Name
Method names must be lowerCamelCase.
7. Constant Name
Constant names should be all UPPER_CASE, separated by underscores.
8. Parameter Name
Parameter names are written in lowerCamelCase.
We have listed a couple of points here. Refer Google JavaScript Style Guide for more points.
Thank you for reading. Happy Coding!