JavaScript Operators
JavaScript operators are used to perform operations on values and variables — such as arithmetic, assignment, comparison, and more. Let’s break them down with simple examples.
JavaScript Assignment
The assignment operator =
assigns a value to a variable:
let x = 10; // Assign 10 to x
let y = 2; // Assign 2 to y
let z = x + y; // Assign 12 to z
JavaScript Addition
let x = 5;
let y = 2;
let z = x + y; // Result is 7
JavaScript Multiplication
let x = 5;
let y = 2;
let z = x * y; // Result is 10
Types of JavaScript Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- String Operators
- Logical Operators
- Bitwise Operators
- Ternary Operator
- Type Operators
JavaScript Arithmetic Operators
Used for basic math:
let a = 3;
let x = (100 + 50) * a; // Result is 450
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
** | Exponentiation (Power) |
++ | Increment |
-- | Decrement |
JavaScript Assignment Operators
let x = 10;
x += 5; // Same as x = x + 5
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
JavaScript Comparison Operators
Return true
or false
based on comparison:
let age = 20;
console.log(age >= 18); // true
JavaScript Logical Operators
&&
AND: true if both are true||
OR: true if one is true!
NOT: reverses boolean
let isLoggedIn = true;
let isAdmin = false;
console.log(isLoggedIn && isAdmin); // false
JavaScript String Operator
The +
operator also joins (concatenates) strings:
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
JavaScript Bitwise Operators
Operate on binary numbers (less common in day-to-day JS):
&
AND|
OR^
XOR~
NOT<<
Left Shift>>
Right Shift
JavaScript Ternary Operator
A shorthand for if...else:
let age = 16;
let result = (age >= 18) ? "Adult" : "Minor";
JavaScript Type Operators
typeof
checks the type, instanceof
checks object type:
typeof "Hello" // "string"
typeof 123 // "number"
[] instanceof Array // true