Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Javascript Syntax and Rules

In this article let us look at Syntax and Basic Rules, which we need to follow while writing JavaScript code. You can refer to the tutorial Hello World example using JavaScript to learn how to create and run a Javascript Program.

Case-sensitivity

JavaScript is case-sensitive. This means that foo is not the same as Foo. Test is not same as test.

Statements & Expressions

The JavaScript has two syntactic distinctions. one is statement & the other one is expression.

Statements

A JavaScript statement performs some action but does not produce any value. Optionally they end with a semicolon.

A Typical Javascript program consists of several such sequences of statements and they control the flow of the program. The JavaScript interpreter executes these statements one by one in the order they are specified in the Program.

For Loops and if statements are examples of statements.

    
//statement that create a variable
var message;      

//statement that assigns “Hello World” to message variable
message=”hello world”; 

//statmenent that prints out console log
console.log(message);

//

    
    

Semi-Colon

; the semicolon is used to indicate the end of a statement.

For Example


var message;
message=”hello world”;
console.log(message);



But, they are optional if you use a single line for each statement

For Example, this is also valid

    
var message
message=”hello world”
console.log(message)




But, they are required if you have multiple statements in a line

For Example

   
var message ; message=”hello world” ; console.log(message) ;




They are also required in some cases where interpreter fails to correctly identify the end of statement.

For Example

   
var c = 1 + 2
(1 + 2).toString()




Expressions

An expression is any valid unit of code that resolves to a value. They can appear anywhere in the code. They can be part of the function arguments or right side of an assignment, etc. The Expressions are often part of a statement.

When interpreter sees an expression it retrieves its value and replaces expression with new value. The following codes are examples of the expressions

                               
//Simple Expression that produces numeric value 5
5;         
 
// Airthmetic Expression that produces numeric value 15   
5+15;     
 
 
//String expression that evaluates to 'hello world' string 
'hello' + 'world'; //
 
//Argument in a function call are expressions
addNum(5,15)
 

                            
                            
                        

Whitespace and Line Breaks

You can use spaces, tabs, and newlines anywhere in the Javascript Program. The Javascript interpreter ignores them. Use tabs & spaces to neatly format or indent your code. It makes the code easy to read & understand.

Comments

The JavaScript allows us to add single line comments or Multi line comments

Single-line comments ( // ) − Any text between a // and the end of a line is treated as a comment.

                            
//this is a single-line comment 

                            
                            
                        

Nested comments are not supported

                              
/*
  /*This Comments not allowed*/
*/