const PI = 3.14159;
// PI = 3; // This would cause an error!
console.log(PI);
Result: Click to see!
3. Var - Function Scoped
var name = "Alice";
name = "Bob"; // We can change var variables
console.log(name);
Result: Click to see!
4. Scope Example
let outside = "I'm outside!";
{
let inside = "I'm inside!";
console.log(inside); // Works
}
// console.log(inside); // Would cause error - inside is not defined
Result: Click to see!
рд░реЛрдЬрдЪреНрдпрд╛ рдЬреАрд╡рдирд╛рддреАрд▓ рдЙрджрд╛рд╣рд░рдгреЗ (Real Life Examples in Marathi)
1. let рдЪреЗ рдЙрджрд╛рд╣рд░рдг - рдкреИрд╢рд╛рдВрдЪреА рдмреЕрдЧ:
// Write your solution here:
const studentId = "ST001"; // Cannot be changed
let mathGrade = 85; // Can be updated
var totalStudents = 30; // Old style variable
// Try changing the values:
mathGrade = 90; // This works
totalStudents = 31; // This works
// studentId = "ST002"; // This would cause error!
Exercise 2: Shopping Cart
Create variables for a shopping cart:
Use const for shop name
Use let for cart total (changes with items)
Use let for number of items
// Your solution here:
const shopName = "Super Market";
let cartTotal = 0;
let itemCount = 0;
// Add items:
itemCount = itemCount + 1; // Add one item
cartTotal = cartTotal + 50; // Add item price