JavaScript Fundamentals & How It Works
Before writing a single line of code, understanding how JavaScript runs is what separates average candidates from great ones. Interviewers at top companies always probe this layer.
What is JavaScript?
JavaScript is a high-level, single-threaded, interpreted (or JIT-compiled) scripting language. It is the only language natively understood by web browsers and is also widely used on the server (Node.js).
The JavaScript Engine
Popular engines include V8 (Chrome, Node.js), SpiderMonkey (Firefox), and JavaScriptCore (Safari). The engine:
- Parses source code into an Abstract Syntax Tree (AST)
- Compiles it to bytecode / machine code
- Executes it
Execution Context & Call Stack
Every time JS runs, a Global Execution Context is created. When a function is called, a new Function Execution Context is pushed onto the Call Stack. When the function returns, it is popped off.
function greet(name) {
return 'Hello, ' + name;
}
console.log(greet('Alice')); // Call stack: [global] -> [greet] -> [global]
Scope Chain & Lexical Environment
Every execution context has a Lexical Environment (variable bindings + reference to outer environment). When a variable is accessed, JS walks up the scope chain.
Interview Tip
Interviewers often ask: "What happens when you call a function in JavaScript?" Walk them through execution context creation, the call stack push, hoisting, and cleanup on return.