
Let’s go step by step through the Syntax in JavaScript.
✍️ JavaScript Syntax
Syntax = The set of rules that define how JavaScript programs are written and understood.
👉 If grammar is for English, syntax is grammar for code.
1️⃣ JavaScript Program Structure
- A program is made of statements (instructions).
- Statements are usually written one per line.
- They can end with a semicolon ( ; ) (optional, but recommended).
let x = 5; // Statement 1
let y = 10; // Statement 2
let sum = x + y; // Statement 3
console.log(sum); // Output: 15
2️⃣ Case Sensitivity
- JavaScript is case-sensitive.
myVar
andmyvar
are different.
let name = "Raj";
let Name = "Kumar";
console.log(name); // Raj
console.log(Name); // Kumar
3️⃣ Comments
Used to explain code (ignored by the browser).
// Single-line comment
/*
Multi-line
comment
*/
4️⃣ Identifiers (Names)
- Names for variables, functions, etc.
- Must start with a letter,
_
, or$
. - Cannot start with a number.
let myName = "Raj";
let $salary = 5000;
let _age = 25;
5️⃣ Variables
Declaring and assigning values:
var oldWay = "not recommended";
let modernWay = "better";
const constantValue = 3.14;
6️⃣ Data Types
let str = "Hello"; // string
let num = 42; // number
let isTrue = true; // boolean
let x; // undefined
let y = null; // null
let arr = [1, 2, 3]; // array (object)
7️⃣ Operators
let a = 10, b = 5;
console.log(a + b); // 15 (Addition)
console.log(a - b); // 5 (Subtraction)
console.log(a * b); // 50 (Multiplication)
console.log(a / b); // 2 (Division)
console.log(a % b); // 0 (Modulus)
8️⃣ Control Flow (Conditions & Loops)
if (a > b) {
console.log("a is bigger");
} else {
console.log("b is bigger");
}
for (let i = 0; i < 3; i++) {
console.log(i);
}
9️⃣ Functions
Reusable code blocks.
function greet(name) {
return "Hello " + name;
}
console.log(greet("Raj"));
Arrow function (modern):
const add = (x, y) => x + y;
console.log(add(2, 3)); // 5
🔟 Objects & Arrays
let person = { name: "Raj", age: 20 };
console.log(person.name); // Raj
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[1]); // Banana
✅ Summary
JavaScript syntax includes:
- Statements (end with
;
) - Case-sensitive keywords & identifiers
- Comments (
//
,/* */
) - Variables (
var
,let
,const
) - Data types (string, number, boolean, object…)
- Operators (
+ - * / %
) - Control structures (
if
,else
,for
,while
) - Functions (
function
, arrow functions) - Objects & arrays