Syntax represents the formal set of grammatical rules that define how JavaScript programs are constructed, structured, and interpreted by the JavaScript engine. Mastering these fundamentals prevents cryptic syntax errors and ensures clean, readable, and maintainable source code.
studentName is completely distinct from StudentName.;).An identifier is a unique developer-assigned name given to variables, functions, classes, or object properties. You must adhere to these syntax rules:
| Syntax Rule | Valid Identifiers | Invalid Identifiers (Syntax Error) |
|---|---|---|
| First Character | score, _userId, $totalPrice |
1stRank (Cannot begin with number!) |
| Subsequent Characters | user2, student_roll_no, sum$ |
user-name (Hyphens are minus operators!) |
| Reserved Keywords | myClass, forLoopCounter |
class, return, let, const (Reserved) |
| Standard Convention | camelCase for variables & functions;PascalCase for Classes; UPPER_SNAKE for constants. |
Inconsistent mixing of casing styles. |
Understanding the distinction between statements and expressions is fundamental across all JavaScript programming:
// 1. EXPRESSIONS (Always evaluate to a value)
5 + 10; // Evaluates to 15
"Hello " + "World"; // Evaluates to "Hello World"
Math.max(10, 40); // Evaluates to 40
true && false; // Evaluates to false
// 2. STATEMENTS (Perform actions or control flow; do NOT produce a value)
let marks = 85; // Variable declaration statement
if (marks > 50) { // Conditional branch statement
console.log("Passed");
}
for (let i = 0; i < 5; i++) {} // Loop iteration statement
JavaScript features a built-in mechanism called ASI (Automatic Semicolon Insertion) that attempts to insert missing semicolons automatically at line breaks. However, blind reliance on ASI can trigger disastrous bugs:
// THE ASI TRAP: Unexpected return behavior
function getUserData() {
return // ASI automatically inserts a semicolon here: return;
{
name: "Vikram"
};
}
console.log(getUserData()); // OUTPUT: undefined (NOT the object!)
// CORRECT WAY: Keep opening bracket on same line
function getCorrectUserData() {
return {
name: "Vikram"
};
}
console.log(getCorrectUserData()); // OUTPUT: { name: "Vikram" }
Introduced in ECMAScript 5, Strict Mode changes previously silent errors into loud exceptions, prevents unsafe coding practices, and secures your code against obsolete quirks:
"use strict"; // Placed at the top of file or function
// 1. Prevents accidental global variable declaration
// Without strict mode, this creates a leaking global variable:
// message = "Hello";
// In strict mode: ReferenceError: message is not defined!
let message = "Hello, Strict Mode";
// 2. Prevents deleting non-configurable properties
// delete Object.prototype; // TypeError in strict mode!
// 3. Forbids duplicate parameter names
// function sum(a, a, b) {} // SyntaxError in strict mode!
function sum(a, b, c) {
return a + b + c;
}
Exam Question: "What are the advantages of enabling Strict Mode in JavaScript?"
with statements and octal numeric literals (e.g. 010).this from automatically defaulting to the global window object in standalone functions (defaults to undefined).Now that you have mastered JavaScript syntax and grammar, let us examine how data is stored and scoped in memory. Proceed to: JavaScript Variables: let vs const vs var →