Name | Progress |
---|---|
JavaScript for Beginners Introduction | Not Read |
Setting up environment | Not Read |
Name | Progress |
---|---|
Section 1 | Not Read |
Section 2 | Not Read |
Section 3 | Not Read |
Module 1 Quiz | Not Attempted |
Name | Progress |
---|---|
Section 1 | Not Attempted |
Section 2 | Not Attempted |
In this lesson, we will explore how to declare variables in JavaScript using let and const. Understanding these keywords is crucial for writing clean and effective code in JavaScript.
Definition:
Examples:
let x = 100;
In this example, x is declared with let and initialized with the value 100. x can be reassigned within its scope.
const y = 150;
Here, y is declared with const and set to 150. Unlike let, y cannot be reassigned to a new value.
Outputs:
❗Note: It is important to understand that while let allows reassignment, any const variable does not. Attempting to reassign a const variable will result in a TypeError.
let x = 100;
x = 200; // This is allowed
const y = 150;
y = 200; // This will throw a TypeError: Assignment to constant variable.