JS: Syntax | JavaScript

JavaScript Syntax

var s = "syntax";//one line comments

JavaScript syntax is very close to C, Java, and C# etc.

JavaScript Variable declaration

var x = 0;

In the example above var is the keyword to declare a variable, x is the identifier, = is the assignement operator and 0 is the value being assigned to x

JavaScript statement

JavaScript statments may be seperated with semicolon ; the end:

var str = 'PiTribe'; console.log('str:', str); str: PiTribe

or each statment per line:

var str = 'PiTribe' console.log('str:', str) str: PiTribe
This is recommended to use a semicolon ; to for a clean and readable code and avoid any confusion.

JavaScript comments

JavaScript comments used for the programmer's memory when reviewing or modifing the code. There are two types of comments:

  • Singlie-line comments, after //
  • Multi-line comments within /* and */
//This is single-line comment, anything in rest of the line will not be executed by the JavaScript interpreter /* This is multi-line comment, anything within slash-asteric and asteric-slash, will not be executed by the JavaScript interpreter */

Prefer single-line comment over multi-line comment

Singlie-line comment // and multi-line comment /**/ have different reasons to use. In case have to comment a piece of code for some reason, prefer single-line comments because if you have already multi-line comments /**/ closed this will override the effect.

  1. JavaScript statments can be ended with ; or new line;
  2. Coments can be single-line // or multi-line /**/