🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 6 Min Lesezeit
0

From Variables to Closures

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




🚀 JavaScript Fundamentals (Week-03): Understanding the Concepts That Every Developer Should Know




"Writing JavaScript code is one thing, but understanding what happens behind the scenes is what makes you a better developer."




When I first started learning JavaScript, I knew how to declare variables and write functions. However, I often found myself asking questions like:




  • Why are there three ways to declare variables?

  • What exactly is hoisting?

  • How does JavaScript execute my code?

  • Why can an inner function access variables from its parent function?

  • What does the this keyword actually refer to?

  • Why do developers keep talking about writing clean code?



This week, I focused on understanding these core JavaScript concepts instead of simply memorizing syntax.



In this article, I'll explain each concept in a beginner-friendly way with examples and practical explanations.









📚 Topics Covered




  • Variables (var, let, const)

  • Hoisting

  • Lexical Scope

  • Execution Context

  • Call Stack

  • Closures


  • this Binding

  • DRY Principle

  • KISS Principle



Let's start from the beginning.









📦 Variables in JavaScript






What is a Variable?



A variable is a named container used to store data in memory.



Instead of writing the same value repeatedly, we store it inside a variable and reuse it whenever required.



For example,




CODE
let name = "Sai";

console.log(name);






Output




CODE
Sai






Here,





  • let → Variable declaration keyword


  • name → Variable name


  • "Sai" → Stored value









Why Do We Need Variables?



Imagine writing this:




CODE
console.log("Sai");
console.log("Sai");
console.log("Sai");






If the value changes, every occurrence must be updated.



Using variables,




CODE
let name = "Sai";

console.log(name);
console.log(name);
console.log(name);






Now changing one line updates every usage.



Variables improve:




  • Readability

  • Reusability

  • Maintainability









Types of Variables



JavaScript provides three ways to declare variables.




  • var

  • let

  • const



Although all three create variables, they behave differently.









var



var was introduced in the first version of JavaScript.



Characteristics:




  • Function Scoped

  • Can be redeclared

  • Can be reassigned

  • Hoisted



Example




CODE
var city = "Hyderabad";

var city = "Bangalore";

console.log(city);






Output




CODE
Bangalore






Because var allows redeclaration, it can accidentally overwrite existing values.









let



let was introduced in ES6.



Characteristics




  • Block Scoped

  • Cannot be redeclared

  • Can be reassigned



Example




CODE
let age = 20;

age = 21;

console.log(age);






Output




CODE
21












const



const is used when the variable should not be reassigned.



Characteristics




  • Block Scoped

  • Cannot be redeclared

  • Cannot be reassigned




CODE
const PI = 3.14;

console.log(PI);






Output




CODE
3.14












📈 Hoisting






What is Hoisting?



Hoisting is JavaScript's behavior of processing declarations before executing the code.



This doesn't mean the code physically moves. Instead, during the memory creation phase, JavaScript prepares variables and functions before execution begins.



Example




CODE
console.log(language);

var language = "JavaScript";






Output




CODE
undefined






Internally JavaScript treats it like this:




CODE
var language;

console.log(language);

language = "JavaScript";






Variables declared using let and const are also hoisted, but they remain inside the Temporal Dead Zone (TDZ) until they are initialized.









🌍 Lexical Scope



Scope determines where a variable can be accessed.



Lexical Scope means an inner function can access variables declared in its outer function because of where it is written.



Example




CODE
function outer(){

let message="Hello";

function inner(){

console.log(message);

}

inner();

}

outer();






Output




CODE
Hello






JavaScript first searches inside inner(). If the variable isn't found, it looks in the outer function.



This searching process is called the Scope Chain.









⚙️ Execution Context



Before JavaScript executes any code, it creates an Execution Context.



Think of it as JavaScript's workspace.



It stores:




  • Variables

  • Functions

  • Scope

  • Value of this



Every Execution Context has two phases.






Memory Creation Phase



During this phase,




  • Variables are allocated memory.


  • var variables are initialized with undefined.

  • Function declarations are stored.






Execution Phase



During this phase,




  • Variables receive values.

  • Statements execute one by one.

  • Function calls create new execution contexts.









📚 Call Stack



The Call Stack keeps track of function execution.



It follows the Last In, First Out (LIFO) principle.



Example




CODE
function first(){

second();

}

function second(){

console.log("Inside Second");

}

first();






Output




CODE
Inside Second






Execution Flow




CODE
first()



second()



console.log()



Return



Return












🔒 Closures



One of JavaScript's most powerful features is the Closure.



A Closure is created when an inner function remembers variables from its outer function even after the outer function has finished execution.



Example




CODE
function counter(){

let count = 0;

return function(){

count++;

console.log(count);

}

}

const increment = counter();

increment();

increment();

increment();






Output




CODE
1
2
3






Closures are commonly used for:




  • Counters

  • Data Privacy

  • Event Handlers

  • Module Pattern









🎯 Understanding this



The this keyword refers to the object that calls a function.



Its value depends on how the function is called.






Implicit Binding






CODE
const user = {

name:"Sai",

greet(){

console.log(this.name);

}

}

user.greet();






Output




CODE
Sai












Explicit Binding



JavaScript provides call(), apply(), and bind() to explicitly decide what this should refer to.




CODE
function greet(){

console.log(this.name);

}

const user={

name:"Sai"

};

greet.call(user);






Output




CODE
Sai












new Binding






CODE
function Student(name){

this.name=name;

}

const s1=new Student("Sai");

console.log(s1.name);






Output




CODE
Sai












Arrow Function



Arrow functions don't have their own this.



Instead, they inherit it from their surrounding scope.









✨ DRY Principle



DRY stands for



Don't Repeat Yourself



Instead of repeating code,




CODE
console.log("Welcome Sai");

console.log("Welcome Rahul");






write




CODE
function greet(name){

console.log(`Welcome ${name}`);

}

greet("Sai");

greet("Rahul");






Benefits




  • Reusable code

  • Easy maintenance

  • Less duplication









💡 KISS Principle



KISS stands for



Keep It Simple, Stupid



Simple code is easier to understand than complicated code.



Instead of




CODE
function isEven(num){

if(num%2===0){

return true;

}

else{

return false;

}

}






write




CODE
function isEven(num){

return num%2===0;

}






Simple code is:




  • Easy to debug

  • Easy to maintain

  • Easy to understand









🎯 Key Takeaways



After completing this week's learning, I understood that:




  • Variables store data.

  • Hoisting happens during memory creation.

  • Lexical Scope decides where variables are accessible.

  • Execution Context provides the environment for code execution.

  • The Call Stack manages function execution.

  • Closures preserve variables after a function finishes.

  • The value of this depends on how a function is called.

  • Following DRY and KISS helps write clean and maintainable code.









📝 Conclusion



Learning JavaScript isn't just about writing code—it's about understanding how the language works behind the scenes. Concepts like execution context, scope, closures, and this binding explain why our code behaves the way it does. By combining these fundamentals with clean coding principles like DRY and KISS, we can write code that is not only correct but also easy to read, maintain, and extend.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten From Variables to Closures

Thematisch verwandte Begriffe: From, Variables, Closures · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...