support Click to see our new support page.

JavaScript Coding Conventions

JavaScript Coding Conventions
Author

IjasSept. 18, 2019

JavaScript Coding

With two decades of improvement, JavaScript has become one of the most popular programming languages of all time. This blogs describes the javaScript coding convention and rules for use in JavaScript programming.

The long-term value of software to an organization is in direct proportion to the quality of the codebase. Over its lifetime, a program will be handled by many pairs of hands and eyes. If a program is able to clearly communicate its structure and characteristics, it is less likely that it will break when modified in the never-too-distant future. JavaScript coding convention can help in reducing the brittleness of programs. They are style guidelines for programming. They typically cover naming and also declaration rules for variables and functions. Rules for the use of white space,indentation ,and comments, programming practices and principles. It improves code readability and make code maintenance easier.

javaScript Coding Convention

Always use the same coding convention for all your JavaScript projects. The javaScript coding convention secure quality. They are style guidelines for programming.

JavaScript Files

JavaScript programs should be store in and deliver as .js files. The JavaScript code should not be embed in HTML files unless the code is specific to a single session.

Naming Conventions

  • Names should be form from the 26 upper and lower case letters (A .. Z, a .. z), the 10 digits (0 .. 9), and _underbar. Avoid use of international characters because they may not read well or be understood everywhere. Do not use $dollar sign or \backslash in names.
  • Do not use _underbar as the first or last character of a name. It is sometimes intended to indicate privacy, but it does not actually provide privacy. If privacy is important, use closure. Avoid conventions that demonstrate a lack of competence.
  • Most variables and functions should start with a lower case letter.
  • Global variables should be avoid, but when used should be in ALL_CAPS.

Variable Declarations

All variables should be declare before use. JavaScript does not require this, but doing so makes the program easier to read and makes it easier to detect undeclared variables that may become implied. Implied global variables should never be used. Use of global variables should be minimized.

variable-declare-javascript

 

Whitespace

Blank lines improve readability by setting off sections of code that are logically related. Blank spaces should always be use in the following circumstances:

  • A keyword follow by (left parenthesis should be separated by a space. Spaces are use to make things that are not invocations look less like invocations, so for example, there should be space after if or while.
    while (true) {
  • A blank space should not be use between a function value and its invoking (left parenthesis. This helps to distinguish between keywords and function invocations.
  • The word function is always follow with one space.
  • No space should separate a unary operator and its operand except when the operator is a word such as typeof.
  • All binary operators should be separate from their operands by a space on each side except .period and (left parenthesis and [left bracket.
  • Every , comma should be follow by a space or a line break.
  • Each ; semicolon at the end of a statement should be follow with a line break.
  • Each ; semicolon in the control part of a for statement should be follow with a space.

Comments

Be generous with comments. It is useful to leave information that will be read at a later time by people (possibly your future self) who will need to understand what you have done and why. The comments should be well-written and clear

It is important that comments be keep up-to-date. Make comments meaningful.

javascript coding convention-comments

Function Declarations

All functions should be declare before they are used. Inner functions should come after the outer function's variable declarations. This helps in making it clear what variables will be include in its scope.

There should be no space between the name of a function and the (left parenthesis of its parameter list. There should be one space between the )right parenthesis and the {left curly brace that begins the statement body. The body itself is indent four spaces. The }right curly brace is align with the line containing the beginning of the declaration of the function.

javascript coding convention-function-declare

Statements

Simple Statements

Each line should contain at most one statement. Put a ;semicolon at the end of every statement that does not end with a {block}. Note that an assignment statement that is assigning a function literal or object literal is still an assignment statement and must end with a semicolon.

Compound Statements

Compound statements are statements that contain lists of statements enclosed in { }curly braces.

  • The enclose statements should be indent four more spaces.
  • The {left curly brace should be at the end of the line that begins the compound statement.
  • The }right curly brace should begin a line and be indent to align with the beginning of the line containing the matching {left curly brace.
  • Braces should be use around all statements, even single statements, when they are part of a control structure, such as an if or for statement. This makes it easier to add statements without accidentally introducing bugs.

if Statement

A if statement should have one of these forms:

if (condition) {
statements
}

if (condition) {
statements
} else {
statements
}

if (condition) {
statements
} else if (condition) {
statements
} else {
statements
}

for Statement

When the for statement is in use, it should one of these forms:

for (initialization; condition; update) {
statements
}

for (
initialization;
condition;
update
) {
statements
}

while Statement

A while statement should have the following form:

while (condition) {
statements
}

do Statement

A do statement should have this form:

do {
statements
} while (condition);

Unlike the other compound statements, the do statement always ends with a ;semicolon.

switch Statement

A switch statement should be avoid, but when in use should have this form:

switch (expression) {

case expression:
statements
default:
statements
}

try Statement

The try statement should have this form:

try {
statements
} catch (variable) {
statements
}

Loading JavaScript in HTML

Use simple syntax for loading external scripts :

<script src="myscript.js"></script>

In the above blog, we explain about JavaScript and its Coding Convention and how it is facilitated in Technaureus Info Solutions Pvt.Ltd.

odoo_erp_services

LinkedIn LinkedIn

Leave a Comment