본문으로 건너뛰기

Variable Assignment

Variable Assignment

In programming, 'assignment' simply refers to the act of 'putting something into a box'.

Here, the box is what we call a 'variable', and the something can be various types of data like numbers or strings.


How do we assign?

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

Example 1:

let fruit = 'apple';

In the code above, we've put the word (value) 'apple' into the box (variable) called fruit.

This is how we use the = symbol to assign the string value 'apple' to the variable fruit.


Example 2:

let age = 20;

Here, we've put the numeric value 20 into the box called age. In other words, we've assigned the number 20 to the variable age.


Can we assign multiple times?

Variables declared with var and let can be reassigned.

let color = 'blue';
color = 'red';

Initially, we put the value 'blue' into the box called color.

But in the next line, we put in the value 'red'. By doing this, the value of color becomes 'red', and the previous value 'blue' is overwritten.


Points of Caution

Variables created with const cannot be changed once a value has been assigned. This is called 'immutability'.

const birthYear = 2000;
birthYear = 2001; // Error occurs!

Summary

'Assignment' is a very important concept in programming. Simply put, it can be thought of as the act of 'putting something into a box'. Assignment stores information in variables, which can then be used later on. 😊

다음 내용이 궁금하다면?

월 12,500원 PLUS 멤버십 가입 or 강의를 등록해 주세요!