- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Assignment Operators
JavaScript Assignment Operators
The assignment operators in JavaScript are used to assign values to the variables. These are binary operators. An assignment operator takes two operands, assigns a value to the left operand based on the value of right operand. The left operand is always a variable and the right operand may be literal, variable or expression.
let x = 10; // right operand is a literal let y = x; // right operand is a variable let z = x + 10; // right operand is an expression
An assignment operator first evaluates the expression and then assign the value to the variable (left operand).
A simple assignment operator is equal (=) operator. In the JavaScript statement "let x = 10;", the = operator assigns 10 to the variable x.
We can combine a simple assignment operator with other type of operators such as arithmetic, logical, etc. to get compound assignment operators. Some arithmetic assignment operators are +=, -=, *=, /=, etc. The += operator performs addition operation on the operands and assign the result to the left hand operand.
Arithmetic Assignment Operators
In this section, we will cover simple assignment and arithmetic assignment operators. An arithmetic assignment operator performs arithmetic operation and assign the result to a variable. Following is the list of operators with example −
Assignment Operator | Example | Equivalent To |
---|---|---|
= (Assignment) | a = b | a = b |
+= (Addition Assignment) | a += b | a = a + b |
-= (Subtraction Assignment) | a -= b | a = a – b |
*= (Multiplication Assignment) | a *= b | a = a * b |
/= (Division Assignment) | a /= b | a = a / b |
%= (Remainder Assignment) | a %= b | a = a % b |
**= (Exponentiation Assignment) | a **= b | a = a ** b |
Simple Assignment (=) Operator
A simple assignment (=) operator assigns a value to a variable. We can assign a single value to multiple variables. This is known as assignment chaining.<html> <body> <div id="output"></div> <script> let x = 5; let y = x + 10; document.getElementById("output").innerHTML = "Value of x : " + x + "<br>" + "Value of y : " + y; </script> </body> </html>
Below is an example of assignment chaining −
<html> <body> <div id="output"></div> <script> let x = y = 5; document.getElementById("output").innerHTML = "Value of x : " + x + "<br>" + "Value of y : " + y; </script> </body> </html>
Addition Assignment (+=) Operator
The JavaScript addition assignment operator performs addition on the two operands and assigns the result to the left operand. Here addition may be numeric addition or string concatenation.
x += b;
In the above statement, it adds values of b and x and assigns the result to x.
Example: Numeric Addition Assignment
<html> <body> <div id="output"></div> <script> let x = 5; x += 7; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Example: String Concatenation Assignment
<html> <body> <div id="output"></div> <script> let x ="Hello"; x += " World"; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Subtraction Assignment (-=) Operator
The subtraction assignment operator in JavaScript subtracts the value of right operand from the left operand and assigns the result to left operand (variable).
let x -=b;
In the above statement, it subtracts b from x and assigns the result to x.
<html> <body> <div id="output"></div> <script> let x = 15; x -= 5; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Multiplication Assignment (*=) Operator
The multiplication assignment operator in JavaScript multiplies the both operands and assign the result to the left operand.
let x *= b;
In the above statement, it multiplies x and b and assigns the result to x.
<html> <body> <div id="output"></div> <script> let x = 10; x *= 5; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Division Assignment (/=) Operator
This operator divides left operand by the right operand and assigns the result to left operand.
let x /= b;
In the above statement, it divides x by b and assigns the result (quotient) to x.
<html> <body> <div id="output"></div> <script> let x = 10; x /= 5; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Remainder Assignment (%=) Operator
The JavaScript remainder assignment operator performs the remainder operation on the operands and assigns the result to left operand.
let x %= b;
In the above statement, it divides x by b and assigns the result (remainder) to x.
<html> <body> <div id="output"></div> <script> let x = 12; x %= 5; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Exponentiation Assignment (**=) Operator
This operator performs exponentiation operation on the operands and assigns the result to left operand.
let x **= b;
In the above statement, it computes x**b and assigns the result to x.
<html> <body> <div id="output"></div> <script> let x = 5; x **= 3; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
JavaScript Bitwise Assignment operators
A bitwise assignment operator performs bitwise operation on the operands and assign the result to a variable. These operations perform two operations, first a bitwise operation and second the simple assignment operation. Bitwise operation is done on bit-level. A bitwise operator treats both operands as 32-bit signed integers and perform the operation on corresponding bits of the operands. The simple assignment operator assigns result is to the variable (left operand).
Following is the list of operators with example −
Assignment Operator | Example | Equivalent To |
---|---|---|
&= (Bitwise AND Assignment) | a &= b | a = a & b |
|= (Bitwise OR Assignment) | a |= b | a = a | b |
^= (Bitwise XOR Assignment) | a ^= b | a = a ^ b |
Bitwise AND Assignment Operator
The JavaScript bitwise AND assignment (&=) operator performs bitwise AND operation on the operands and assigns the result to the left operand (variable).
let x &= b;
In the above statement, it performs bitwise AND on x and b and assigns the result to the variable x.
<html> <body> <div id="output"></div> <script> let x = 7; x &= 5; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Bitwise OR Assignment Operator
The JavaScript bitwise OR assignment (|=) operator performs bitwise OR operation on the operands and assigns the result to the left operand (variable).
let x |= b;
In the above statement, it performs bitwise OR on x and b and assigns the result to the variable x.
<html> <body> <div id="output"></div> <script> let x = 7; x |= 5; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Bitwise XOR Assignment Operator
The JavaScript bitwise XOR assignment (^=) operator performs bitwise XOR operation on the operands and assigns the result to the left operand (variable).
let x ^= b;
In the above statement, it performs bitwise XOR on x and b and assigns the result to the variable x.
<html> <body> <div id="output"></div> <script> let x = 7; x ^= 5; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
JavaScript Shift Assignment Operators
A shift assignment operator performs bitwise shift operation on the operands and assign the result to a variable (left operand). These are a combinations two operators, the first bitwise shift operator and second the simple assignment operator.
Following is the list of the shift assignment operators with example −
Assignment Operator | Example | Equivalent To |
---|---|---|
<<= (Left Shift Assignment) | a <<= b | a = a << b |
>>= (Right Shift Assignment) | a >>= b | a = a >> b |
>>>= (Unsigned Right Shift Assignment) | a >>>= b | a = a >>> b |
Left Shift Assignment Operator
The JavaScript left shift assignment (<<=) operator performs left shift operation on the operands and assigns the result to the left operand (variable).
let x <<= b;
In the above statement, it performs left shift on x and b and assigns the result to the variable x.
<html> <body> <div id="output"></div> <script> let x = 7; x <<= 2; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Right Shift Assignment Operator
The JavaScript right shift assignment (>>=) operator performs right shift operation on the operands and assigns the result to the left operand (variable).
let x >>= b;
In the above statement, it performs right shift on x and b and assigns the result to the variable x.
<html> <body> <div id="output"></div> <script> let x = 7; x >>= 1; document.getElementById("output").innerHTML = "Value of x : " + x; </script> </body> </html>
Unsigned Right Shift Assignment Operator
The JavaScript unsigned right shift assignment (>>>=) operator performs unsigned right shift operation on the operands and assigns the result to the left operand (variable).
let x >>>= b;
In the above statement, it performs unsigned right shift on x and b and assigns the result to the variable x.
<html> <body> <div id="output"></div> <script> let x = 7; x >>>= 2; document.getElementById("output").innerHTML ="Value of x : " + x; </script> </body> </html>
JavaScript Logical Assignment operators
In JavaScript, a logical assignment operator performs a logical operation on the operands and assign the result to a variable (left operand). Each logical assignment operator is a combinations two operators, the first logical operator and second the simple assignment operator.
Following is the list of the logical assignment operators with example −
Assignment Operator | Example | Equivalent To |
---|---|---|
&&= (Logical AND Assignment) | a &&= b | a = a && b |
||= (Logical OR Assignment) | a ||= b | a = a || b |
??= (Nullish Coalescing Assignment) | a ??= b | a = a ?? b |
Example
<html> <body> <div id="output"></div> <script> var a = 5; var b = 10; var result = (a &&= b); document.getElementById("output").innerHTML = "Value of (a &&= b) => " + result + "<br/>"; result = (a ||= b); document.getElementById("output").innerHTML += "Value of (a ||= b) => " + result; </script> </body> </html>