JavaScript operators are special symbols or keywords that perform operations on one or more operands (data values) and produce a result. Understanding operators is fundamental for effective JavaScript programming as they are used in almost every aspect of the language, from simple arithmetic to complex logical operations.
Types of JavaScript Operators
JavaScript operators can be categorized into several types:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
- Conditional (Ternary) Operator
Type Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numbers.
Addition (+): Adds two operands.
let y = 10;
let result = x + y; // 15
Subtraction (-): Subtracts the second operand from the first.
Multiplication (*): Multiplies two operands.
Division (/): Divides the first operand by the second.
Modulus (%): Returns the remainder of a division.
Exponentiation ()**: Raises the first operand to the power of the second operand.
Increment (++): Increases an integer value by one.
Decrement (--): Decreases an integer value by one.
2. Assignment Operators
Assignment operators are used to assign values to variables.
Assignment (=): Assigns the right operand value to the left operand.
Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
Multiplication Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
Modulus Assignment (%=): Takes modulus using the two operands and assigns the result to the left operand.
Exponentiation Assignment (=)**: Raises the left operand to the power of the right operand and assigns the result to the left operand.
3. Comparison Operators
Comparison operators are used to compare two values and return a boolean value (true or false).
Equal to (==): Checks if two values are equal.
Not equal to (!=): Checks if two values are not equal.
Strict equal to (===): Checks if two values are equal in value and type.
Strict not equal to (!==): Checks if two values are not equal in value or type.
Greater than (>): Checks if the left value is greater than the right value.
Less than (<): Checks if the left value is less than the right value.
Greater than or equal to (>=): Checks if the left value is greater than or equal to the right value.
Less than or equal to (<=): Checks if the left value is less than or equal to the right value.
4. Logical Operators
Logical operators are used to combine or invert boolean values.
Logical AND (&&): Returns true if both operands are true.
Logical OR (||): Returns true if at least one operand is true.
Logical NOT (!): Inverts the boolean value of the operand.
5. Bitwise Operators
Bitwise operators perform operations on the binary representations of numbers.
AND (&): Returns a 1 in each bit position where both corresponding bits are 1.
OR (|): Returns a 1 in each bit position where at least one of the corresponding bits is 1.
XOR (^): Returns a 1 in each bit position where the corresponding bits are different.
NOT (~): Inverts all the bits of the operand.
Left Shift (<<): Shifts the bits of the operand to the left by the specified number of positions.
Right Shift (>>): Shifts the bits of the operand to the right by the specified number of positions.
Unsigned Right Shift (>>>): Shifts the bits of the operand to the right by the specified number of positions, filling in zeros from the left.
6. String Operators
String operators are used to perform operations on strings.
Concatenation (+): Joins two or more strings together.
Concatenation Assignment (+=): Appends the right operand to the left operand and assigns the result to the left operand.
greeting += " World!"; // "Hello World!"
7. Conditional (Ternary) Operator
The conditional (ternary) operator assigns a value to a variable based on a condition.
Syntax: condition ? expressionIfTrue : expressionIfFalse
8. Type Operators
Type operators are used to determine or change the type of a value.
typeof: Returns a string indicating the type of the operand.
instanceof: Tests whether an object is an instance of a constructor.
void: Evaluates an expression without returning a value.
Practice Excercise Practice now