Lecture

Variable Assignment

In programming, assignment refers to the act of placing data into a variable.

Once a value is assigned to a variable, you can retrieve and use it later when needed.


How Do You Assign a Value?

In JavaScript (and most programming languages), the = symbol is used to assign values.

Assigning the string 'apple' to the fruit variable
let fruit = 'apple'; console.log(fruit); // 'apple'

In this code, the box (variable) named fruit contains the word 'apple' (value).

We use the = symbol here to assign the string value 'apple' to the variable fruit.


Assigning the number 20 to the age variable
let age = 20;

Here, we're putting the number 20 into the box named age. In other words, we're assigning the number 20 to the variable age.


Can You Assign a Value Multiple Times?

Variables declared with var and let can be reassigned.

Reassigning the color variable
let color = 'blue'; color = 'red';

Initially, the box color contains the value 'blue'.

On the next line, we put the value 'red' into it. This means color now holds 'red', and the previous value 'blue' is gone.


Important Note

Variables created with const cannot be reassigned once a value has been assigned. This is known as being 'immutable.'

Variables declared with const cannot be reassigned
const birthYear = 2000; birthYear = 2001; // Error occurs!

Summary

In simple terms, 'assignment' can be thought of as the act of placing information into a box.

Assignment allows you to store information in a variable and reuse that information later.

Mission
0 / 1

How do you assign a value to a variable in JavaScript?

Use the = symbol to assign the left side value to the right.

Use the = symbol to assign the right side value to the left.

Use the : symbol to assign the left side value to the right.

Use the : symbol to assign the right side value to the left.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run

Execution Result