Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
YouTube Security Videosheise & c't: #heiseshow: EU Kids Act, Claude Opus 5.5, IT-Arbeitsmarkt(23.09.2026 um 15:45 Uhr)
YouTube Security VideosHow to quickly blur faces in video | Intel(23.09.2026 um 16:05 Uhr)
YouTube Security VideosGoogle Cloud Tech: Join Developers Building with Gemini(23.09.2026 um 15:00 Uhr)
YouTube Security VideosGoogle Workspace: Micro habits 🤏Macro results 🚀 #Shorts(23.09.2026 um 15:15 Uhr)
Windows Tipps & SecurityLenovo ThinkPad X9 15p Aura Edition Review ⭐(23.09.2026 um 15:58 Uhr)
YouTube Security Videosheise & c't: #heiseshow: EU Kids Act, Claude Opus 5.5, IT-Arbeitsmarkt(23.09.2026 um 15:45 Uhr)
YouTube Security VideosHow to quickly blur faces in video | Intel(23.09.2026 um 16:05 Uhr)
YouTube Security VideosGoogle Cloud Tech: Join Developers Building with Gemini(23.09.2026 um 15:00 Uhr)
YouTube Security VideosGoogle Workspace: Micro habits 🤏Macro results 🚀 #Shorts(23.09.2026 um 15:15 Uhr)
Windows Tipps & SecurityLenovo ThinkPad X9 15p Aura Edition Review ⭐(23.09.2026 um 15:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

`var`, `let`, and `const` in JavaScript

What is a Variable? A variable is a named container used to store data in a program. The stored value can be a number, string, object, array, or any other data type. Example: let name = "John"; let age = 25; 1.…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




What is a Variable?



A variable is a named container used to store data in a program. The stored value can be a number, string, object, array, or any other data type.



Example:




let name = "John";
let age = 25;












1. var



var is the oldest way to declare variables in JavaScript. It was used before the introduction of ES6 (ECMAScript 2015).






Syntax






var city = "Chennai";









Characteristics




  • Function-scoped

  • Can be redeclared

  • Can be reassigned

  • Hoisted and initialized with undefined






Example






var language = "JavaScript";
console.log(language);

language = "Python";
console.log(language);

var language = "Java";
console.log(language);






Output:




JavaScript
Python
Java









Scope Example






if (true) {
var message = "Hello";
}

console.log(message);






Output:




Hello






Although message is declared inside the if block, it is still accessible outside because var is function-scoped, not block-scoped.









2. let



let was introduced in ES6 and is now the preferred way to declare variables whose values may change.






Syntax






let score = 90;









Characteristics




  • Block-scoped

  • Cannot be redeclared in the same scope

  • Can be reassigned

  • Hoisted but not initialized (Temporal Dead Zone)






Example






let marks = 80;

marks = 95;

console.log(marks);






Output:




95









Block Scope Example






if (true) {
let number = 10;
console.log(number);
}

console.log(number);






Output:




10
ReferenceError: number is not defined






The variable exists only inside the block where it is declared.









3. const



const is also introduced in ES6 and is used for values that should not be reassigned.






Syntax






const PI = 3.14159;









Characteristics




  • Block-scoped

  • Cannot be redeclared

  • Cannot be reassigned

  • Must be initialized during declaration






Example






const country = "India";

console.log(country);






Attempting to reassign it:




country = "USA";






Output:




TypeError: Assignment to constant variable.









Objects with const



A const object cannot be reassigned, but its properties can be modified.




const student = {
name: "Alex",
age: 20
};

student.age = 21;

console.log(student);






Output:




{
name: "Alex",
age: 21
}












Difference Between var, let, and const


















































Feature var let const
Scope Function Block Block
Redeclaration Yes No No
Reassignment Yes Yes No
Hoisted Yes Yes Yes
Initialized During Hoisting Yes (undefined) No No
Must Initialize No No Yes








Hoisting Example






console.log(a);
var a = 5;






Output:




undefined









console.log(b);
let b = 5;






Output:




ReferenceError









console.log(c);
const c = 5;






Output:




ReferenceError












When Should You Use Each?






Use const when:




  • The variable value should not change.

  • Declaring constants like API URLs or configuration values.



Example:




const company = "OpenAI";









Use let when:




  • The value needs to change later.



Example:




let count = 0;

count++;

console.log(count);









Avoid var in modern JavaScript because:




  • It ignores block scope.

  • It allows accidental redeclaration.

  • It can introduce bugs in large applications.









Best Practices




  • Use const by default.

  • Use let only when the value needs to change.

  • Avoid using var in modern JavaScript development.

  • Give variables meaningful names.

  • Keep variable scope as small as possible.









Conclusion



Understanding the differences between var, let, and const is essential for writing reliable JavaScript code. While var was widely used in older JavaScript versions, modern development favors let and const because they provide block scope and help prevent common programming mistakes.



As a general rule:




  • Use const for values that should remain unchanged.

  • Use let for variables whose values will change.

  • Avoid var unless you're maintaining legacy JavaScript code.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten `var`, `let`, and `const` in JavaScript

Thematisch verwandte Begriffe: const, JavaScript · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-92164 | Streamlink is a CLI utility which pipes video streams from various servi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick