Most people begin JavaScript by building things. You wire up a button, fetch some data, and watch the DOM change. Then the abstractions leak. Bugs surface that make no sense: variables exist before their declarations, functions remember variables they should not have access to, and the this keyword points at the window, a button, or nothing at all. That is usually the moment you realize you need to look under the hood and understand what the engine is actually doing.
Execution Context: The Two-Phase Setup
When the JavaScript engine in your browser or Node.js encounters a script, it does not simply read the file from top to bottom like a person scanning a page. Instead, it builds an execution context, a container that holds everything needed to run a particular chunk of code. Every execution context moves through two distinct phases.
Memory Creation Phase. During this initial pass, the engine scans the entire scope and allocates memory for every variable and function declaration it finds. If it sees a var, it reserves space and stores undefined as a placeholder. If it sees a function declaration, it stores the complete function body. This is why a function declared with the traditional function keyword can be called from lines that appear earlier in the same scope. The engine already knows about it before it starts executing.
Code Execution Phase. Now the engine runs your code line by line. Assignments happen here. Expressions are evaluated. Functions are invoked. If you wrote var name = "Alice";, the placeholder from phase one finally gets replaced with the string. Understanding this two-pass behavior clears up a surprising amount of confusion. The engine is not sloppy; it is following a strict setup routine.
Variables and the Temporal Dead Zone
Choosing between var, let, and const is not just a stylistic preference. var is function-scoped, which means it ignores braces entirely. Declare it inside an if block, and it leaks outward. That behavior might have made sense in early JavaScript, but in modern applications it causes real maintenance headaches. let and const are block-scoped. They respect curly braces and disappear when the block ends.
There is also a subtle but crucial difference in how hoisting treats them. var declarations are hoisted and immediately initialized with undefined. let and const are technically hoisted too; the engine knows they exist before it reaches the declaration line. But they are not initialized. They sit in a limbo called the temporal dead zone. If you try to read them before the declaration line executes, you get a hard ReferenceError rather than a sneaky undefined. That crash is actually helpful. It prevents logic from proceeding on top of uninitialized data.
Lexical Scope and Closures
Scope answers a simple question: where can I access this variable? JavaScript uses lexical scope, which means a function's access rights are decided by where it was physically written in the source code, not by where it ends up being called. If you define a function inside another function, the inner one can reach outward to read variables from its parent. The outer function cannot reach inward. This relationship is static. You could pass that inner function across modules, store it in a global variable, and call it from an entirely different file. It still remembers the variables from the scope where it was born.
That behavior naturally produces closures. A closure forms when an inner function keeps a reference to a variable from its outer scope. Even after the outer function finishes executing and its local variables should have been garbage collected, JavaScript preserves them in memory because the inner function still needs them. The inner function carries its surrounding environment with it.
This is not merely an academic detail. Closures give you a practical way to create private state in a language that lacks explicit access modifiers.
function makeCounter() {
let count = 0;
return function() {
count = count + 1;
return count;
};
}
const counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2
Here, count is hidden. Nothing outside the returned function can reset it or read it directly. That is a private variable built from scope mechanics alone.
Hoisting in Practice
JavaScript "அறிவிப்புகளை (declarations) மேலே நகர்த்துகிறது" என்று கேட்பது வழக்கம். அது புரிந்துகொள்ள ஒரு பயனுள்ள முறைதான், ஆனால் குறியீடு உண்மையில் மாற்றப்படுவதில்லை. நினைவக உருவாக்க நிலையில் (memory creation phase), இயக்கம் தொடங்குவதற்கு முன்பே எஞ்சின் (engine) அறிவிப்புகளைப் பதிவு செய்கிறது. var x = 5; போன்ற ஒரு கூற்று, அறிவிப்பும் தொடக்கமும் (initialization) பிரிக்கப்பட்டிருப்பதைப் போலச் செயல்படுகிறது. var x; என்ற அறிவிப்பு முன்கூட்டியே செயலாக்கப்பட்டு undefined எனத் தொடக்க மதிப்பைத் பெறுகிறது. x = 5; என்ற ஒதுக்கீடு (assignment) நீங்கள் எழுதிய இடத்திலேயே இருக்கும் மற்றும் செயல்படுத்தும் நிலையில் (execution phase) இயங்கும்.
இதன் காரணமாக, var ஆச்சரியமான முடிவுகளைத் தரலாம். ஒரு செயல்பாட்டின் (function) தொடக்கத்தில் பயன்படுத்தப்படும் ஒரு மாறி, அதன் இறுதியில் ஒரு பெரிய ஒதுக்கீடு இருந்தாலும் undefined ஆக இருக்கலாம். let மற்றும் const ஆகியவற்றைப் பயன்படுத்துவது இத்தகைய சிக்கல்களைத் தவிர்க்கிறது, ஏனெனில் 'temporal dead zone' உங்கள் அறிவிப்புகளைப் பயன்பாட்டிற்கு மேலே வைத்திருக்கக் கட்டாயப்படுத்துகிறது.
this அதன் மதிப்பை எவ்வாறு பெறுகிறது
மாறிகள் எங்கு இருக்கின்றன என்பதை 'execution context' மற்றும் 'scope' தீர்மானித்தால், எந்தப் பொருள் (object) தற்போது பொறுப்பில் உள்ளது என்பதை this தீர்மானிக்கிறது. லெக்சிகல் மாறிகளைப் (lexical variables) போலல்லாமல், this என்பது ஒரு செயல்பாடு எங்கு எழுதப்பட்டுள்ளது என்பதன் மூலம் தீர்மானிக்கப்படுவதில்லை. அது அந்தச் செயல்பாடு எவ்வாறு அழைக்கப்படுகிறது என்பதன் மூலம் மட்டுமே தீர்மானிக்கப்படுகிறது.
Default binding என்பது நீங்கள் ஒரு சாதாரண, தனித்த செயல்பாட்டை (standalone function) அழைக்கும்போது நிகழ்கிறது. Non-strict mode-இல், this உலகளாவிய பொருளாக (global object) மாறும். ஒரு உலாவியில் (browser), அது window ஆகும். எந்தவொரு சூழலும் இன்றி ஒரு செயல்பாட்டை அழைத்தால், நீங்கள் அறியாமலேயே உலகளாவிய நிலையை (global state) மாற்றக்கூடும்.
Implicit binding என்பது நீங்கள் ஒரு பொருளின் முறையாக (method on an object) ஒரு செயல்பாட்டை அழைக்கும்போது நிகழ்கிறது. நீங்கள் user.sayName() என்று எழுதினால், அந்த அழைப்பின் கால அளவிற்கு this-ஐ user என்று அமைக்க எஞ்சினுக்கு அந்த புள்ளி (dot) அமைதியாகக் கூறுகிறது. செயல்பாட்டின் வரையறை இருக்கும் இடத்தை விட, அது அழைக்கப்படும் இடமே முக்கியமானது.
Explicit binding என்பது அனைத்தையும் கைமுறையாக மாற்றியமைக்க (override) அனுமதிக்கிறது. call() மற்றும் apply() ஆகியவை நீங்கள் வழங்கும் ஒரு குறிப்பிட்ட பொருளாக this-ஐத் திணிக்கும் அதே வேளையில், ஒரு செயல்பாட்டை உடனடியாக அழைக்கின்றன. அவற்றுக்கிடையேயான ஒரே வித்தியாசம் ஆர்குமெண்ட்களை (arguments) எவ்வாறு அனுப்புவது என்பதுதான்: call என்பது கமா மூலம் பிரிக்கப்பட்ட பட்டியலை ஏற்கும், அதே சமயம் apply ஒரு அணியை (array) ஏற்கும். bind() வேறு விதமாகச் செயல்படுகிறது. இது செயல்பாட்டை உடனடியாக அழைக்காது. அதற்குப் பதிலாக, நீங்கள் வழங்கிய மதிப்பிற்கு this-ஐ நிரந்தரமாகப் பூட்டி வைக்கும் ஒரு புதிய செயல்பாட்டை இது வழங்கும். இது callbacks-களுக்கு மிகவும் பயனுள்ளது, இல்லையெனில் அவை இடமாற்றம் செய்யப்படும்போது அவற்றின் சூழலை (context) இழக்கக்கூடும்.
New binding என்பது ஒரு செயல்பாட்டிற்கு முன்னால் new என்ற முக்கியச் சொல்லைப் பயன்படுத்தும்போது செயல்படுகிறது. எஞ்சின் ஒரு புதிய காலியான பொருளை உருவாக்கி, அதன் புரோட்டோடைப் இணைப்பை (prototype linkage) அமைத்து, கன்ஸ்ட்ரக்டருக்குள் (constructor) உள்ள this-ஐ அந்தப் புதிய இன்ஸ்டன்ஸிற்கு (instance) சுட்டிக்காட்டுகிறது.
நீடித்து நிலைக்கக்கூடிய குறியீட்டை எழுதுதல்
இயக்கவியலைப் (mechanics) புரிந்துகொள்வது கைத்திறனின் பாதி மட்டுமே. மற்ற பாதி, ஆறு மாதங்களுக்குப் பிறகு மனிதர்கள் படிக்கக்கூடிய குறியீட்டை எழுதுவதாகும்.
DRY (Don't Repeat Yourself) என்பது எளிமையானதாகத் தோன்றலாம், ஆனால் இது தொடர்ந்து மீறப்படுகிறது. மூன்று வெவ்வேறு கோப்புகளில் ஒரே மாதிரியான சரிபார்ப்பு தர்க்கத்தை (validation logic) அல்லது API அழைப்பு முறையை நீங்கள் எழுதுகிறீர்கள் என்றால், அதைத் தனியாகப் பிரித்தெடுக்கவும். ஒரு செயல்பாட்டை எழுதுங்கள். உண்மையின் ஒரே ஆதாரம் (one source of truth) என்பது தேவைகள் மாறும்போது மாற்றங்களைச் செய்ய ஒரே இடம் இருக்கும் என்று அர்த்தம், இது நேரத்தை மிச்சப்படுத்துகிறது.
KISS (Keep It Simple, Stupid) என்பது அகந்தைக்கு எதிரான ஒரு பாதுகாப்பு அரண். நெஸ்டட் டெர்னரிகள் (Nested ternaries) மற்றும் ஒற்றை வரி க்ளோஷர்கள் (one-liner closures) புத்திசாலித்தனமாகத் தோன்றலாம், ஆனால் அவை பிழைத்திருத்தத்தின் (debugging) போது பல மணிநேரங்களைச் செலவழிக்கும். நான் முன்பு காட்டிய க்ளோஷர் (closure) முறை சக்தி வாய்ந்தது, ஆனால் உங்களால் முடியும் என்பதற்காகவே ஐந்து நிலைகள் வரை நெஸ்டிங் (nesting) செய்வது ஒரு தவறு. எளிமையான குறியீடு குழு மாற்றங்கள், தயாரிப்புச் சிக்கல்கள் (production incidents) மற்றும் எதனால் உடைந்தது என்று யாருக்கும் தெரியாத நள்ளிரவு எச்சரிக்கைகள் ஆகியவற்றின் போதும் நிலைத்து நிற்கும்.
உண்மையான பலன்
செயல்படுத்தும் முறையை (execution)
