JavaScript is a client – and server-side scripting language for creating interactive web pages with the help of CSS and HTML.

As developers, we have to follow some coding standards so that it’s easy to understand if someone else reviews our code, and if everyone follows those standards, then code consistency exists. Consistency of code is the main factor.

Spacing in JavaScript:

  1. You must indent with tabs, not Spaces.

  2. There should be no Spaces or blank lines at the end of the line.

  3. Lines should not exceed 100 characters, including tabs.

  4. Commas (,) and semicolons (;) Must not contain preceding Spaces.

  5. Block statements such as if, else, while, and try must have multiple lines and always use braces.

  6. Operands used with unary special character operators cannot be preceded by Spaces, or we can say unary special character operators cannot be followed by Spaces.

    支那

    Example:

    支那

    • ++a;

    • + + a; (Bad practice)

  7. Do you like the ternary operators? And: must have Spaces on both sides

Object:

If the object is short, such as 100 characters per line, you must declare the object in a line. If the line length exceeds, each line must have one attribute.

Example:

  • var user = { name: charu, age: 25, status: single };

  • Var num =,4,15 [9];

  • var user = { name: charu,age: 25, status: single }; (Bad practice)

  • Var num = [9, 4,15]; (Bad practice)

Indentation:

To avoid the difficulty of reading complex statements, we can use indentation and line breaks so that our program must be easy to read.

We should use tabs to indent properly.

Example:

  • function( a, b ) { if( a > b ) { alert( a ); } else { alert( b ); }}
  • function( a, b ) { if( a > b ) { alert(a) } else { alert(b) } (bad practice) }

Naming conventions

Variable and function names must be descriptive. For example, if we define a function that calculates percentages, the function name should be like calculatePercentage() so that the user can easily understand the purpose of the function, as well as the variable name.

Variable and function names must begin with a lowercase letter and use camel case when necessary.

example

  • Var userName = "charu";
  • function sum() { var num1 = 10; var num2 = 10; var total = num1 + num2; }
  • Var ABC = "charu"; (bad practice)
  • function hhgh() { var num1 = 10; var num2 = 10; var total = num1 + num2; (bad practice) }

An array of

In JavaScript, we have to use the shorthand constructor [] to create arrays instead of using the new array() notation.

example

  • **Var userArray = []; 支那
  • Var userArray = [' charu ', 'priya']; 支那