
JavaScript Part 2: let, const & Data Types
1. let — The Modern Variable let was introduced in ES6 (2015) and is now the preferred way to declare variables that can change their value. let age = 20 ; console . log ( age ); // 20 age = 21 ; // ✅ you can update it console . log ( age ); // 21 Why let over var ? The key difference is scope — let is block-scoped , meaning it only exists within the {} block it was defined in. if ( true ) { let message = " Hi! " ; console . log ( message ); // ✅ works here } console . log ( message ); // ❌ ReferenceError — not accessible outside With var , the same code would work outside the block — which often causes unexpected bugs. let keeps things predictable and safe. 2. const — The Constant Use const when the value should never change after it's set. const PI = 3.14159 ; console . log ( PI ); // 3.14159 PI = 3 ; // ❌ TypeError — you cannot reassign a const Must be initialised immediately Unlike let , you cannot declare a const without giving it a value. const country ; // ❌ SyntaxError const co
Continue reading on Dev.to Webdev
Opens in a new tab


