Curriculum
Module 10 // Core JavaScript
Literals & Syntax Rules
Module Objective
Object {}, array [], function syntax, template literals, numeric literals, regex literals, strict vs sloppy mode
Mental Model Realtime Simulation
INTERACTIVE_CANVASEditor_Pane
Loading...
Console_Output
Waiting for output...
Practical Code Examples
// Example 1
// 1. Object Literal Enhancements
const name = "Subhajit";
const age = 25;
const user = {
name, // Property shorthand
age,
sayHi() { // Method shorthand
console.log("Hi!");
}
};
console.log(user);💡 Modern JS allows shorthand property and method names in object literals, making code much more concise.
// Example 2
// 2. Strict Mode Benefits
"use strict";
function mistake() {
// x = 10; // ❌ ReferenceError (No global leak)
let x = 10; // ✅ Correct
}
const obj = {};
Object.defineProperty(obj, "readOnly", { value: 1, writable: false });
// obj.readOnly = 2; // ❌ TypeError in Strict Mode💡 `"use strict"` catches common coding mistakes and prevents the creation of accidental global variables, making your code more secure and optimized.
// Example 3
// 3. Numeric Literals
const binary = 0b1010; // 10
const octal = 0o744; // 484
const hex = 0xFF; // 255
const big = 1_000_000; // ✅ Numeric separators for readability
console.log(big); // 1000000💡 JavaScript supports various bases for numbers and allows underscores as separators to make large numbers easier to read.
Engine & Memory Architecture
Parsing & Allocation
1. Literal Optimization:
- When the engine sees a literal like
{}, it avoids the overhead of calling a constructor function. - It allocates memory in the Heap instantly based on the literal's shape.
2. Strict Mode Impact:
- Strict mode forces the engine to perform more thorough checks during the Parsing phase.
- It disables certain "bad" features (like
with), allowing the JIT (Just-In-Time) Compiler to optimize the code better.
3. Tokenization:
- The engine breaks your code into tokens (e.g.,
const,identifier,operator). - Literals are identified as fixed values directly in the Abstract Syntax Tree (AST).