Author - StudySection Post Views - 456 views
JavaScript

JavaScript Coding Standards

JavaScript is a client-side as well as a server-side scripting language used to make interactive web pages with the help of CSS and HTML.

There are some coding standards that we must follow as a developer so that if someone else reviews our code it’s easy to understand and if everyone follows these standards then consistency in the code will be there. Consistency in the code is the major factor.

Spacing in JavaScript:

  1. Must use tab for indentation instead of spacebar.
  2. At the end of the line, there should not be whitespaces or blank lines.
  3. Lines should not exceed 100 characters including tabs.
  4. The comma (,) and semicolon (;) must not include preceding space.
  5. Blocks statements like if, else, while, try must go with multiple lines and always use braces.
  6. Operands used with unary special character operators must not have preceding space or we can say there must not be a space after unary special character operators.
    Example:

    • ++a;
    • ++ a ; (bad practice)
  7. Ternary operators like? and: must have spaces on both the sides

Objects:

Objects must be declared in a single line if they are short like 100 characters per line.

If the line length exceeds then there must be one property per line.

Example:

  • var user = { name: charu, age: 25, status: single };
  • var num = [
    9,
    4,
    15
    ];
  • var user = { name: charu,
    age: 25, status: single }; (bad practice)
  • var num = [ 9, 4,
    15 ] ; (bad practice)

Indentation:

To avoid difficulty while reading complex statements, indentation and line breaks can be used so that our program must be read easily. We should make use of tabs for proper indentation.
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 for calculating percentage then the function name should be like calculatePercentage() so that the user can easily understand the purpose of this function and the same in the case of the variable name.
Variable and function names must start with lowercase letters and use camel case where needed.
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)
    }

Arrays

Instead of using a new array() notation for creating an array, we must use a shorthand constructor [ ] to create an array in JavaScript.
Example

  • Var userArray = [];
  • Var userArray = [ ‘charu’, ‘priya’ ];

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri