JavaScript

JavaScript coding Practices | Code structure and Syntax

Every language has some standards that everyone has to follow. As an example, there exist some rules to speak in English such as grammar, vocabulary, etc. The same concept applies in programming languages i.e. every programming language has a syntax to execute and run a program. In this article, we will talk about the rules that we have to follow while dealing with a JavaScript program.

Case Sensitivity in JavaScript

JavaScript is a case-sensitive language, this means when dealing with any built-in property of JavaScript then deal with it carefully.

To better understand this concept consider the below given example:

console.log("Hello World");

Console.log("Hello World");

Both statements are the same except for the first letter of the console. Now the resultant output will verify that the console the first statement will execute successfully while the second statement will through an error:

From the above output, we can analyze that JavaScript is a case-sensitive language. So, we have to follow the proper naming conventions.

However, variable names and function names, there is no such restriction to follow. it is a good convention to use and follow the camel-casing naming convention i.e. varName, funcName.

Statements in JavaScript

In JavaScript, a program is a collection of multiple instructions, each instruction is called a statement in JavaScript. This statement is made up of anything like different characters, keywords, methods, or operators. JavaScript executes each statement step by step.

Let’s consider the below example to figure out how a statement works:

console.log("Hello World");

In this example, there is a statement that tells the browser to log “Hello World” on the console:

Semicolons in JavaScript

In JavaScript, semicolons are used as a separator, for example, we have multiple statements to execute. Then we can’t write each statement one after the other like a paragraph. We have to separate every single instruction with a semicolon. For example:

var a,b,c a=5 b=6 c=10 console.log("value of a : ", a) console.log("value of b : ", b) console.log("value of c : ", c);

We can’t write a program as we did in this example:

In this example, we have 7 statements but we didn’t separate them so this program wouldn’t work, it will through an error:

We need to separate each statement, and we can do it by separating each statement by a new line and it’s a good practice to write only one statement in each line and use semicolon after each statement, as we did in the following example:

var a,b,c;
a=5;
b=6;
c=10;
console.log("value of a : ", a);
console.log("value of b : ", b);
console.log("value of c : ", c);

Now all the seven statements are separated by a semicolon “;” and JavaScript will execute each statement one after the other:

On successful execution, we will get the following output:

However, if the statements are written in separate lines then JavaScript would execute perfectly fine without using semicolons at the end of the statement and throw no error.

White Spaces in JavaScript

JavaScript is not sensitive about white spaces and it ignores extra spaces. Consider the following example to understand the concept of white spaces:

var  a , b  ;
a=        5    ;

    b=  6;

console.log("value of a : ", a);

console.log("value of b : ", b);

In this example, we put numerous white spaces in the form of blank spaces, tabs, and newlines between the different statements:

The output will confirm that JavaScript ignores the extra spaces and prints the output on the console:

Comments in JavaScript

JavaScript provides a structure for the comments, we can “comment” a single line or a segment of code using JavaScript comments. For example:

// console.log("Hello World");

The two forward slashes “//” comment a single line:

For multiline comments in JavaScript use “/*” at the start and “*/” at the end:

/*

var a,b,c;

a=5;

b=6;

c=10;

console.log("value of a : ", a);

console.log("value of b : ", b);

console.log("value of c : ", c);

*/

The code between “/*” and “*/” will be commented:

Reserved Keyword in JavaScript

There are a number of built-in keywords in JavaScript like let, const, while, for, and else. These keywords are used to perform different functionalities. We can’t change their syntax according to our requirements. For example, consider the following example:

let if= 5;

We can’t create a variable with the reserved keyword “if”:

JavaScript wouldn’t assign a value ‘5’ to the variable “if”. Because “if” is reserved for the conditional statements.

Conclusion

Taking care of the syntax rules and following the good conventions while dealing with a JavaScript program is a good practice that every beginner web developer should adapt to. In this article, we studied the code structure and syntax of JavaScript for good coding practices. We learned that JavaScript runs a program from top to bottom and it is not sensitive to whitespaces or line breaks. Then we learn how to comment the unnecessary instructions in a program.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.