JavaScript operators are symbols that perform actions on values and variables. The main types are arithmetic, assignment, comparison, logical, bitwise, string, ternary, and special operators.
Arithmetic operators:
These are used for math operations
- + Addition: 5 + 2 = 7
- - Subtraction: 5 - 2 = 3
- * Multiplication: 5 * 2 = 10
- / Division: 5 / 2 = 2.5
- % Modulus: gives the remainder, 5 % 2 = 1
- ** Exponentiation: 5 ** 2 = 25
Assignment operators:
These assign or update values in variables
- = assign
- += add and assign
- -= subtract and assign
- *= multiply and assign
- /= divide and assign
- %= remainder and assign
Example:
let x = 10;
x += 5; // x becomes 15 but "x=x+5"
Comparison operators:
These compare two values and return true or false
- == equal to
- === equal value and equal type
- != not equal
- !== not equal value or type
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
Example:
console.log(5 === "5"); // false
console.log(5 == "5"); // true
Logical operators:
These are used in conditions
- && logical AND
- || logical OR
- ! logical NOT
- ?? nullish coalescing, gives the right value only
when the left side is null or undefined (TBD)
Bitwise operators:
These work on binary bits of numbers
- & bitwise AND
- | bitwise OR
- ^ bitwise XOR (TBD)
- ~ bitwise NOT (TBD)
- << left shift (TBD)
- >> signed right shif (TBD)
- >>> unsigned right shift (TBD)
String and ternary operators:
JavaScript also has operators for text and short conditional logic
- + can join strings: "Hello" + " World"
- ?: ternary operator: condition ? value1 : value2 (TBD)
Example:
let result = marks >= 40 ? "Pass" : "Fail";
Special operators:(TBD)
These are useful in different situations
typeofchecks the type of a valueinstanceofchecks whether an object is an instance of a class or constructordeleteremoves a property from an objectinchecks whether a property exists in an object?.optional chaining, safely accesses nested values...spread operator, expands arrays or objects.
Top comments (0)