How to Store Values with the Assignment Operator in JavaScript

Posted by

The assignment operator in JavaScript is used to store a value in a variable. The assignment operator is the equal sign (=).

Here is an example of how to use the assignment operator to store a value in a variable:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x;  // Declare a variable named "x"
x = 10;  // Assign the value 10 to x

[/dm_code_snippet]

You can also use the assignment operator to assign a value to a variable when you declare it:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let y = 20;  // Declare a variable named "y" and assign it the value 20

[/dm_code_snippet]

You can also use the assignment operator to assign a value to a variable that has already been declared:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let z;  // Declare a variable named "z"
z = 30;  // Assign the value 30 to z
z = 40;  // Re-assign the value 40 to z

[/dm_code_snippet]

You can also use the assignment operator to assign a value to a variable that has already been declared and initialized:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let a = 50;  // Declare a variable named "a" and assign it the value 50
a = 60;  // Re-assign the value 60 to a

[/dm_code_snippet]

You can also use the assignment operator to assign a value to a variable that has been declared using the const keyword:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

const b = 70;  // Declare a constant named "b" and assign it the value 70

[/dm_code_snippet]

However, you cannot re-assign a value to a constant using the assignment operator. If you try to do so, you will get a TypeError.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

b = 80;  // This will throw a TypeError because you cannot re-assign a value to a constant

[/dm_code_snippet]

You can also use the assignment operator to assign a value to multiple variables at the same time:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let c = 10, d = 20, e = 30;  // Declare three variables and assign them the values 10, 20, and 30, respectively

[/dm_code_snippet]

You can also use the assignment operator to assign a value to a variable that is the result of an expression:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let f = (10 + 20) * 30;  // Declare a variable named "f" and assign it the result of the expression (10 + 20) * 30

[/dm_code_snippet]