Syntax, Comments, Data Types & Variables
JavaScript programs are made of statements. Each statement is one instruction, usually ending with a semicolon (;).
JavaScript Syntax
JavaScript programs are made of statements. Each statement is one instruction, usually ending with a semicolon (;).
JavaScript is case-sensitive — name and Name are different. Spaces and line breaks are mostly ignored.
Real-life example: Statements are like steps in a recipe. Step 1: boil water. Step 2: add tea. Order matters.
let cups = 2;
cups = cups + 1;
console.log(cups); // 3Comments
Comments are notes for humans. The computer skips them. Use // for one line and /* ... */ for many lines.
Real-life example: Comments are like pencil notes in your textbook margin — they help you remember, but the teacher ignores them in the exam.
// This is a single-line comment
let price = 100;
/*
This comment
spans many lines
*/
price = price + 50;Identifiers — naming things
Identifiers are names for variables, functions, and objects. They can use letters, digits, _, and $. They cannot start with a digit.
Use camelCase for multi-word names: userName, totalPrice. Avoid reserved words like let, if, class as names.
Real-life example: An identifier is a name tag. "studentRoll42" is clear. "x" is a name tag with no info — hard to remember later.
const userName = "Asha"; // good
const totalPrice = 499; // good
// const 2name = "bad"; // Error — starts with digitData Types
JavaScript has primitive types (simple values) and objects (collections and complex data). typeof tells you the type.
Real-life example: Data types are like different containers — a water bottle (string), a weight scale reading (number), a yes/no switch (boolean).
- string — text in quotes: "Hello"
- number — integers and decimals: 42, 3.14
- boolean — true or false
- undefined — variable declared but no value yet
- null — empty on purpose
- bigint — very large whole numbers
- symbol — unique identifier (advanced)
- object — arrays, dates, plain objects, etc.
typeof "Rishtaara" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" (known quirk)
typeof 9007199254740991n // "bigint"
typeof Symbol("id") // "symbol"
typeof { name: "A" } // "object"Variables — let, const, and var
Variables store values. Use const when the value will not be reassigned. Use let when it will change. Avoid var in new code.
const does not freeze objects — you can change properties inside an object, but you cannot reassign the variable itself.
Real-life example: const is like your birth date — fixed. let is like today's temperature — it changes through the day.
const siteName = "Rishtaara";
// siteName = "Other"; // Error — cannot reassign const
let score = 0;
score = score + 10; // OK with let
var oldStyle = "avoid in new code";
// var has function scope and hoisting quirks