List of do’s and don’ts in JavaScript for best practices
Today we will provide you the list of the do’s and don’ts in JavaScript for best practices 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.
- How to get query string parameters from URL in JavaScript
- Validate the value by regular expression in JavaScript
- How to detect the user browser in JavaScript
- JavaScript Array methods Splice vs Slice
- Difference between var, let and const 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!