JavaScript Syntax: Statements, Grammar Rules & 'use strict' Mode

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.

Core Structural Rules of JavaScript

  • Case Sensitivity: Identifiers, keywords, variables, and function names are strictly case-sensitive. studentName is completely distinct from StudentName.
  • Whitespace & Line Breaks: JavaScript ignores extra spaces, tabs, and empty lines. You can format code with indentation for maximum human readability.
  • Character Set: JavaScript source code uses the universal Unicode (UTF-8) character set, supporting international alphabets, math symbols, and emojis.
  • Statements: JavaScript programs are sequences of executable statements separated by semicolons (;).

Rules for Naming Identifiers (Variables & Functions)

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.

Statements vs. Expressions (Crucial Concept)

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

Automatic Semicolon Insertion (ASI) & The Semicolon Trap

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" }

Strict Mode ('use strict')

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;
}

University Exam Corner (BCA / B.Tech IT)

Exam Question: "What are the advantages of enabling Strict Mode in JavaScript?"

  • Catches accidental assignments to undeclared variables (eliminates global leakage).
  • Disallows duplicate function parameter names that cause silent overwrites.
  • Prohibits obsolete legacy features such as with statements and octal numeric literals (e.g. 010).
  • Prevents this from automatically defaulting to the global window object in standalone functions (defaults to undefined).

Next Step

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 →

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!