JavaScript Syntax

JavaScript Syntax

What Is JavaScript Syntax?

JavaScript syntax is the set of rules that tells us:

  • How to write JavaScript code
  • How the browser or Node.js understands the code
  • How instructions are executed step by step

If syntax rules are broken, JavaScript will throw an error and the code will not run.


JavaScript Statements

A statement is a single instruction given to JavaScript.

Each statement tells JavaScript to do one specific task.

Example:

console.log("Hello JavaScript");

This statement tells JavaScript to:

  • Print “Hello JavaScript” in the console

You can write multiple statements, and JavaScript executes them line by line.

let x = 10;
let y = 20;
console.log(x + y);

Semicolons in JavaScript

A semicolon (;) is used to mark the end of a statement.

Example:

let name = "Rahul";
console.log(name);

Important Note:

  • JavaScript can automatically insert semicolons (Automatic Semicolon Insertion)
  • But using semicolons is a good practice
  • It avoids unexpected errors in complex code

Recommendation: Always use semicolons.


Indentation in JavaScript

Indentation means adding spaces or tabs to make code clean and readable.

JavaScript does not require indentation to run, but it is very important for humans to understand the code.

Example without indentation (bad practice):

if(true){
console.log("Hello");
}

Example with proper indentation (good practice):

if (true){    
    console.log("Hello");
}

Benefits of Indentation:

  • Improves readability
  • Makes debugging easier
  • Follows industry coding standards

📣 Follow us for more updates:

Follow on LinkedIn Join WhatsApp Channel
Scroll to Top