Operators are essential components in programming languages, including JavaScript. They are symbols or keywords used to perform operations on variables and values. These operations can range from simple arithmetic calculations to complex logical comparisons.
An operator is a symbol or function that performs a specific operation on one or more operands (values or variables). Operators are fundamental to programming, allowing developers to manipulate data, control the flow of programs, and make decisions based on conditions.
In JavaScript, operators are used to perform a variety of tasks, including:
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
Purpose: Perform basic mathematical operations.
| Operator | Description | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 6 / 3 | 2 |
| % | Modulus | 5 % 3 | 2 |
| ++ | Increment | x++ (where x=5) | 6 |
| -- | Decrement | x-- (where x=5) | 4 |
Purpose: Compare two values and return a boolean result (true or false).
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 5 > 3 | true |
| < | Less than | 5 < 3 | false |
| >= | Greater than or equal to | 5 >= 5 | true |
| <= | Less than or equal to | 5 <= 3 | false |
Purpose: Operate on the binary representations of numbers.
| Operator | Description | Example | Result |
|---|---|---|---|
| & | Bitwise AND | 5 & 3 | 1 |
| | | Bitwise OR | 5 | 3 | 7 |
| ^ | Bitwise XOR | 5 ^ 3 | 6 |
| ~ | Bitwise NOT | ~5 | -6 |
| << | Left shift | 5 << 1 | 10 |
| >> | Right shift | 5 >> 1 | 2 |
Purpose: Combine or negate boolean values.
| Operator | Description | Example | Result |
|---|---|---|---|
| && | Logical AND | true && false | false |
| || | Logical OR | true || false | true |
| ! | Logical NOT | !true | false |
Purpose: Assign values to variables and perform operations.
| Operator | Description | Example | Result |
|---|---|---|---|
| = | Assignment | x = 5 | x is 5 |
| += | Addition assignment | x += 5 (if x = 10) | 15 |
| -= | Subtraction assignment | x -= 5 (if x = 10) | 5 |
| *= | Multiplication assignment | x *= 5 (if x = 10) | 50 |
| /= | Division assignment | x /= 5 (if x = 10) | 2 |
| %= | Modulus assignment | x %= 5 (if x = 10) | 0 |
Purpose: Provide additional functionality.
| Operator | Description |
|---|---|
| (?:) | The conditional (ternary) operator evaluates a condition and returns one value if true and another if false. |
| , | The comma operator evaluates multiple expressions and returns the result of the last expression. |
| delete | The delete operator removes a property from an object, or an element from an array. |
| in | The in operator checks if a specified property exists in an object or its prototype chain. |
| instanceof | The instanceof operator checks whether an object is an instance of a specific constructor function. |
| new | The new operator creates a new instance of an object based on a constructor function. |
| typeof | The typeof operator returns a string indicating the type of a variable or expression. |
| void | The void operator evaluates an expression and returns undefined, effectively discarding the result. |
| yield | The yield operator is used within a generator function to pause execution and return a value to the generator's iterator. |