JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.
- = : Assigns the value on the right to the variable on the left.
- Example:
x = y
meansx
is assigned the value ofy
.
- Example:
- += : Adds the right operand to the left operand and assigns the result to the left operand.
- Example:
x += y
is the same asx = x + y
.
- Example:
- -= : Subtracts the right operand from the left operand and assigns the result to the left operand.
- Example:
x -= y
is the same asx = x - y
.
- Example:
- *= : Multiplies the left operand by the right operand and assigns the result to the left operand.
- Example:
x *= y
is the same asx = x * y
.
- Example:
- /= : Divides the left operand by the right operand and assigns the result to the left operand.
- Example:
x /= y
is the same asx = x / y
.
- Example:
- %= : Calculates the remainder of dividing the left operand by the right operand and assigns the result to the left operand.
- Example:
x %= y
is the same asx = x % y
.
- Example:
- **= : Raises the left operand to the power of the right operand and assigns the result to the left operand.
- Example:
x **= y
is the same asx = x ** y
- Example:
Shift Assignment Operators
JavaScript also includes bitwise shift assignment operators, which are used to shift the bits of the first operand, then assign the result back to that operand. Here's how each works:
- <<= (Left Shift Assignment): Shifts the bits of the variable on the left operand to the left by the number of positions specified by the right operand, then assigns the result back to the left operand.
- Example:
x <<= y
is the same asx = x << y
.
- Example:
- >>= (Right Shift Assignment): Shifts the bits of the variable on the left operand to the right by the number of positions specified by the right operand, preserving the sign, then assigns the result back to the left operand.
- Example:
x >>= y
is the same asx = x >> y
.
- Example:
- >>>= (Unsigned Right Shift Assignment): Shifts the bits of the variable on the left operand to the right by the number of positions specified by the right operand, filling the new positions with zeros, then assigns the result back to the left operand.
- Example:
x >>>= y
is the same asx = x >>> y
.
- Example:
Bitwise Assignment Operators
- &= (Bitwise AND Assignment): Applies the bitwise AND operation to both operands and assigns the result back to the left operand.
- Example:
x &= y
is the same asx = x & y
.
- Example:
- ^= (Bitwise XOR Assignment): Applies the bitwise XOR operation to both operands and assigns the result back to the left operand.
- Example:
x ^= y
is the same asx = x ^ y
.
- Example:
- |= (Bitwise OR Assignment): Applies the bitwise OR operation to both operands and assigns the result back to the left operand.
- Example:
x |= y
is the same asx = x | y
.
- Example:
Logical Assignment Operators
- &&= (Logical AND Assignment): Assigns the right operand to the left operand only if the left operand is truthy.
- Example:
x &&= y
effectively meansx = x && (x = y)
. It will assigny
tox
ifx
is truthy; otherwise,x
retains its original value.
- Example:
- ||= (Logical OR Assignment): Assigns the right operand to the left operand if the left operand is falsy.
- Example:
x ||= y
translates tox = x || (x = y)
. It will assigny
tox
ifx
is falsy; otherwise,x
retains its value.
- Example:
- ??= (Nullish Coalescing Assignment): Assigns the right operand to the left operand only if the left operand is
null
orundefined
.- Example:
x ??= y
meansx = x ?? (x = y)
. This operator will assigny
tox
ifx
isnull
orundefined
; otherwise,x
remains unchanged.
- Example:
The = Operator
The Simple Assignment Operator (=
) in JavaScript is used to assign values directly to variables. This operator can assign both straightforward values and results of expressions.
Simple Assignment Examples:
let x = 10;
assigns the value10
directly to the variablex
.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
let x = 10 + y;
assigns the result of the expression10 + y
to the variablex
, wherey
needs to be previously defined or declared
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>
<p id="demo"></p>
<script>
let y = 50
let x = 10 + y;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The += Operator
The Addition Assignment Operator (+=
) in JavaScript is used to add a value to a variable and then update the variable with the new sum. This operator simplifies adding a value to a variable and reassigning the result to the same variable in one step.
Addition Assignment Examples:
let x = 10;
sets the initial value ofx
to 10.x += 5;
adds 5 to the current value ofx
, resulting inx
becoming 15
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x += 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
let text = "Hello"; text += " World";
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>
<p id="demo"></p>
<script>
let text = "Hello";
text += " World";
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
The -= Operator
The Subtraction Assignment Operator (-=
) in JavaScript is used to subtract a value from a variable and then automatically update the variable with the new result. This operator efficiently combines a subtraction and an assignment into one operation.
Subtraction Assignment Example:
let x = 10;
initializes the variablex
with the value 10.x -= 5;
then subtracts 5 from the current value ofx
, resulting inx
becoming 5.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Subtraction Assignment</h2>
<h3>The -= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x -= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The *= Operator
The Multiplication Assignment Operator (*=
) in JavaScript multiplies a variable by a value and then updates the variable with the resulting product.
Multiplication Assignment Example:
let x = 10;
sets the initial value ofx
to 10.x *= 5;
multiplies the current value ofx
by 5, resulting inx
becoming 50.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Multiplication Assignment</h2>
<h3>The *= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x *= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The **= Operator
The Exponentiation Assignment Operator (**=
) in JavaScript raises a variable to the power of a specified operand and then updates the variable with the resulting value.
Exponentiation Assignment Example:
let x = 10;
initializesx
with the value 10.x **= 5;
raisesx
to the power of 5, effectively computing 105105, which results inx
becoming 100000.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Exponentiation Assignment</h2>
<h3>The **= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x **= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The /= Operator
The Division Assignment Operator (/=
) in JavaScript divides a variable by a specified value and then updates the variable with the resulting quotient.
Division Assignment Example:
let x = 10;
initializesx
with the value 10.x /= 5;
dividesx
by 5, resulting inx
becoming 2.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Division Assignment</h2>
<h3>The /= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x /= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
The %= Operator
The Remainder Assignment Operator (%=
) in JavaScript calculates the remainder of the division of a variable by a specified value and then updates the variable with that remainder.
Remainder Assignment Example:
let x = 10;
initializesx
with the value 10.x %= 5;
dividesx
by 5 and assigns the remainder of this division tox
, which results inx
becoming 0, because 10 divided by 5 leaves no remainder.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Remainder Assignment</h2>
<h3>The %= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x %= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The <<= Operator
The Left Shift Assignment Operator (<<=
) in JavaScript shifts the bits of a variable to the left by a specified number of positions and assigns the result back to the variable. This bitwise operation effectively multiplies the number by 2 raised to the power of the number of shifts.
Left Shift Assignment Example:
let x = -100;
initializesx
with the value -100.x <<= 5;
shifts the bits ofx
five positions to the left. In binary, left-shifting a number by one position is equivalent to multiplying it by 2, so shifting by 5 positions multiplies it by 2525 or 32. This operation changes the value ofx
to -3200 (since -100 multiplied by 32 equals -3200).
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Left Shift Assignment</h2>
<h3>The <<= Operator</h3>
<p id="demo"></p>
<script>
let x = -100;
x <<= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The Right Shift Assignment Operator (>>=
) in JavaScript shifts the bits of a variable to the right by a specified number of positions and assigns the result back to the variable.
Right Shift Assignment Example:
let x = -100;
initializesx
with the value -100.x >>= 5;
shifts the bits ofx
five positions to the right. This operation divides the number by 2525 or 32, while preserving the sign of the number. Thus, -100 divided by 32 roughly equals -3 when rounded towards zero, resulting inx
becoming -3.
The >>>= Operator
The Unsigned Right Shift Assignment Operator (>>>=
) in JavaScript shifts the bits of a variable to the right by a specified number of positions and assigns the result back to the variable.
Unsigned Right Shift Assignment Example:
let x = -100;
initializesx
with the value -100.x >>>= 5;
shifts the bits ofx
five positions to the right.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The >>>= Operator</h3>
<p id="demo"></p>
<script>
let x = -100;
x >>>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The &= Operator
The Bitwise AND Assignment Operator (&=
) in JavaScript performs a bitwise AND operation between two operands and then assigns the result back to the variable. This operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the resulting bit is set to 1; otherwise, it is set to 0.
Bitwise AND Assignment Example:
let x = 10;
initializesx
with the value 10, which in binary is1010
.x &= 5;
performs a bitwise AND operation with5
, which in binary is0101
.- The operation
1010 & 0101
results in0000
because none of the corresponding bits in both numbers are 1 at the same position. - Thus, after the operation,
x
becomes 0.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise AND Assignment</h2>
<h3>The &= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x &= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The |= Operator
The Bitwise OR Assignment Operator (|=
) in JavaScript performs a bitwise OR on two operands and assigns the result to the variable.
Example:
let x = 10;
x |= 5;
changesx
to 15.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise OR Assignment</h2>
<h3>The |= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x |= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The ^= Operator
The Bitwise XOR Assignment Operator (^=
) performs a bitwise XOR operation between two operands and assigns the resulting value back to the variable.
Example:
let x = 10;
x ^= 5;
changesx
to 15.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise XOR Assignment</h2>
<h3>The ^= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x ^= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The &&= Operator
The Logical AND Assignment Operator (&&=
) assigns the second value to the variable if the first value is truthy.
Example:
let x = 10;
x &&= 5;
results inx
being set to 5, because 10 is truthy.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Logical AND Assignment</h2>
<h3>The &&= Operator</h3>
<p>If the first value is true, the second value is assigned.</p>
<p id="demo"></p>
<script>
let x = 100;
x &&= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The ||= Operator
The Logical OR Assignment Operator (||=
) assigns the second value to the variable if the first value is falsy.
Example:
let x = 10;
- Since
x
is truthy,x ||= 5;
does not changex
, and it remains 10.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Logical OR Assignment</h2>
<h3>The ||= Operator</h3>
<p>If the first value is false, the second value is assigned:</p>
<p id="demo"></p>
<script>
let x = undefined;
x ||= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
The ??= Operator
The Nullish Coalescing Assignment Operator (??=
) assigns the second value to the variable if the first value is either undefined
or null
.
Example:
let x;
initializesx
without a value, so it'sundefined
.x ??= 5;
assigns5
tox
becausex
wasundefined
. Now,x
equals 5.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>The ??= Operator</h2>
<p>The ??= operator is used between two values. If the first value is undefined or null, the second value is assigned.</p>
<p id="demo"></p>
<script>
let x;
document.getElementById("demo").innerHTML = x ??= 5;
</script>
</body>
</html>