Best Practices

1) Execute your JavaScript code in Strict Mode.
Include the "use strict" directive to execute your JavaScript code in Strict Mode
Using strict mode will reduce bugs, improves security and overall performance of your application.

'use strict'

2) Always terminate your statement with a semicolon.
When you do not include them you will be relying on a process called Automatic Semicolon Insertion (ASI).

var1 = 'Mon';

3) Always use double quotes for strings.
You can use single or double quotes, but single quotes are recommended.

let string1 = "some ' text";   
let string2 = 'some " text';

Double quotes eliminate the need to escape apostrophes
Double quotes mean a string in many other languages.
Double quotes is standard in HTML so using single might be less confusing.
Double quotes should be used with JSON.

const jsonString = '{"key1":"value1"}';  

4) Use Template Strings instead of string concatenation.

5) Always use const when declaring variables unless the value is going to be changed later.
Variables should be declared with "const" if they are only going to be assigned a value once.
Variables should only be declared with "let" if the value is going to be changed later on.
Variables declared using "let" and "const" have block scope. Never use 'var'.

6) Always declare your variables close to where they are being used, rather than at the top.
Avoid using single letters for your variable names. Never use "i" and "j" in your loops.
Avoid using any reserved words or names that are similar to object properties or methods.

7) Indent your code using 2 spaces, never tabs.

8) Prefix your local variables with an underscore and give them a descriptive name.

let _firstName; 
let _salary;
let _workbook;

9) Declare all your constants in capitals.

10) Function names, variable names and methods should be in camelCase.
Always start with a lowercase letter.

11) Always use the triple equals ===

if ('100' === 100) {   // false  
if ('100' == 100) { // true

12) Always use the exclamation and double equals !==


13) Always use a Regular Function for object methods and constructors
This is because of how 'this' works.

14) Do not use Anonymous Functions

15) Do not use Anonymous Function Expressions
There is no way to refer to itself from inside the function (so recursion is not possible)
They are much harder to debug
They make your code harder to read

16) All source file names should be in lowercase

17) Never have lines break within a statement

18) Never start a newline with an opening curly parenthesis

19) Never start a newline with an opening square parenthesis

20) When to return and when not to return

© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext