Section 1
by Louis Nguyen· Module 2: Introduction to Variables and Data Types

Root Folder
Not Attempted
NameProgress
JavaScript for Beginners Introduction
Not Read
Setting up environment
Not Read
Module 1: Engaging with JavaScript Fundamentals
Not Attempted
NameProgress
Section 1
Not Read
Section 2
Not Read
Section 3
Not Read
Module 1 Quiz
Not Attempted
Section 1
Arithmetic Operations
Type Conversion
Module 3
Not Attempted
NameProgress
Section 1
Not Attempted
Section 2
Not Attempted
0 / 30 XP

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: 

  • let allows users to declare variables that are limited in scope to the block, statement, or expression where they are used.
  • const  enables the declaration of variables meant to remain constant or unchanged through the script. A const declaration creates a variable that cannot be reassigned after its initialization and shares the block-scoped nature of let.

Examples:

  • Variable declaration with let.
let x = 100;

In this example, x is declared with let and initialized with the value 100. x can be reassigned within its scope.

  • Variable declaration with const
const y = 150;

Here, y is declared with const and set to 150. Unlike let, y cannot be reassigned to a new value.

Outputs: 

  • if you run console.log(x) and console.log(y) are 100 and 150, respectively.

❗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.

 

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline
Module 1: Engaging with JavaScript Fundamentals
Module 2: Introduction to Variables and Data Types
Module 3
Module 4
Module 5