JavaScript Operators
JavaScript operators are symbols used to perform specific mathematical, comparison, assignment, and logical computations on operands. They are fundamental elements in JavaScript programming, allowing developers to manipulate data and control program flow efficiently. Understanding the different types of operators and how they work is important for writing effective and optimized JavaScript code.
JavaScript Operators: There are various operators supported by JavaScript.
Table of Content
- JavaScript Arithmetic Operators
- JavaScript Assignment Operators
- JavaScript Comparison Operators
- JavaScript Logical Operators
- JavaScript Bitwise Operators
- JavaScript Ternary Operators
- JavaScript Comma Operators
- JavaScript Unary Operators
- JavaScript Relational Operators
- JavaScript BigInt Operators
- JavaScript String Operators
JavaScript Arithmetic Operators
JavaScript Arithmetic Operators perform arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and exponentiation (**).
Name |
Description |
Syntax |
Example |
Addition ‘+’ operator performs addition on two operands. This ‘+’ operator can also be used to concatenate (add) strings. |
Y = “Beginner” + “for” + “Beginner” gives Y = “w3wiki” Y |
||
Subtraction ‘-‘ operator performs subtraction on two operands. |
Y = 5 – 3 gives Y = 2 |
||
Multiplication ‘*’ operator performs multiplication on two operands. |
Y = 5 * 5 gives Y = 25 |
||
Division ‘/’ operator performs division on two operands (divide the numerator by the denominator). |
Y = 5 / 5 gives Y = 1 |
||
Modulus ‘%’ operator gives a remainder of an integer division. |
A % B means remainder (A/B) Y = 5 % 4 gives Y = 1 |
||
Exponentiation ‘**’ operator give the power of the first operator raised to the second operator. |
Y = 5 ** 3 gives Y = 125 |
||
Increment ‘+ +’ operator increases an integer value by one. |
let A = 10 and Y = A + + then A = 11, Y=10 |
||
Decrement ‘- -‘ operator decreases an integer value by one. |
let A = 10 and Y = A – – then A = 9, Y=10 |
||
Unary ‘+’ is the fastest and preferred way of converting something into a number |
+a means a is a positive number |
||
Negation ‘-‘ operator gives the negation of an operand. |
-a means a is a negative number |
JavaScript Assignment Operators
The assignment operation evaluates the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables
Name |
Description |
Syntax |
Example |
Assignment (=) |
This operator assigns the right operand value to the left operand. |
If A = 10 and Y = A then Y = 10 |
|
Sums up left and right operand values and then assigns the result to the left operand. |
Y += 1 gives Y = Y + 1 |
||
It subtracts the right side value from the left side value and then assigns the result to the left operand. |
Y -= 1 gives Y = Y – 1 |
||
It multiplies a variable by the value of the right operand and assigns the result to the variable. |
Y *= A is equivalent to Y = Y * A |
||
It divides a variable by the value of the right operand and assigns the result to the variable. |
Y /= A is equivalent to Y = Y / A |
||
It divides a variable by the value of the right operand and assigns the remainder to the variable. |
Y %= A is equivalent to Y = Y % A |
||
This raises the value of a variable to the power of the right operand. |
Y **= A is equivalent to Y=Y ** A |
||
It moves the specified amount of bits to the left and assigns the result to the variable. |
Y <<= A is equivalent to Y=Y << A |
||
It moves the specified amount of bits to the right and assigns the result to the variable. |
Y >>= A is equivalent to Y = Y >> A |
||
: It does a bitwise AND operation on the operand, and assigns the result to the variable. |
Y &= b is equivalent to Y = Y & A |
||
It does a bitwise OR operation on the operand, and assigns the result to the variable. |
Y |= A is equivalent to Y= Y | b |
||
It does a bitwise XOR operation on the operand, and assigns the result to the variable. |
Y ^= A is equivalent to Y= Y ^ A |
JavaScript Comparison Operators
Comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values.
Name |
Description |
Syntax |
Example |
Compares the equality of two operands. If equal then the condition is true otherwise false. |
Y = 5 and X = 6 Y = = X is false. |
||
Compares the equality of two operands with type. If both value and type are equal then the condition is true otherwise false. |
Y = 5 and X = ‘5’ Y = = = X is false. |
||
Compares inequality of two operands. True if operands are not equal. |
let X = 10 then X ! = 11 is true. |
||
Compares the inequality of two operands with type. If both value and type are equal then the condition is true otherwise false. |
let X = 10 then X ! == ’10’ is true. |
||
This operator checks whether the left side value is greater than the right side value. If yes then it returns true otherwise it returns false. |
let X = 10 then X > 11 is false. |
||
This operator checks whether the left side value is less than the right side value. If yes then it returns true otherwise it returns false. |
let X = 10 then X < 11 is true. |
||
This operator checks whether the left side operand is greater than or equal to the right side operand. If yes then it returns true otherwise it returns false. |
let X = 10 then X > = 11 is false. |
||
This operator checks whether the left side operand value is less than or equal to the right side operand value. If yes then it returns true otherwise it returns false. |
let X = 10 then X < = 10 is true. |
JavaScript Logical Operators
JavaScript Logical Operators perform logical operations: AND (&&), OR (||), and NOT (!), evaluating expressions and returning boolean values.
Name |
Description |
Syntax |
Example |
It checks whether two operands are non-zero (0, false, undefined, null, or “” are considered as zero), if yes then return the last operand when evaluating from left to right |
Y = 5 and X = 6 Y && X is 6. |
||
It checks whether two operands are non-zero (0, false, undefined, null, or “” is considered as zero), if yes then return the first operand when evaluating from left to right. |
Y = 5 and X = 0 Y || X is 5. |
||
It reverses the boolean result of the operand (or condition). |
Y = 5 and X = 0 !(Y || X) is false. |
JavaScript Bitwise Operators
The bitwise operator in JavaScript is used to convert the number to a 32-bit binary number and perform the bitwise operation. The number is converted back to the 64-bit number after the result.
Name |
Description |
Syntax |
Example |
The operator returns true only if both the operands are true |
A = 6, B=1 A&B = 0 |
||
The operator returns true even if one of the operands is true |
A = 6, B=1 A|B = 7 |
||
The operator returns true if both operators are distinct |
A = 6, B=1 A^B = 7 |
||
This operator is used to invert the boolean value of the operand |
A = 6 ~A = -7 |
||
In this two operators are used where the first operand is the number and the second operand is the number of bits to shift to the left. |
A = 6, B=1 A<<B = 12 |
||
In this two operators are used where the first operand is the number and the second operand is the number of bits to shift to the right. |
A = 6, B=1 A>>B = 3 |
||
It is same as a bitwise right shift the only difference is that overflowing bits are discarded. |
A = 6, B=1 A>>>B = 3 |
JavaScript Ternary Operators
The ternary operator has three operands. It is the simplified operator of if/else.
Name |
Description |
Syntax |
Example |
It is like the short form of the if-else condition. |
Y = ? A : B If the condition is true then Y = A otherwise Y = B |
JavaScript Comma Operators
Comma Operator (,) mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand.
Name |
Description |
Syntax |
Example |
When a comma operator is placed in an expression, it executes each expression and returns the rightmost expression. |
Expression1, Expression2, Expression3, …so on |
JavaScript Unary Operators
A unary operation is an operation with only one operand.
Name |
Description |
Syntax |
Example |
It returns the operand type, The possible types that exist in javascript are undefined, Object, boolean, number, string, symbol, and function. |
typeof variable |
||
This operator is more specifically used to delete JavaScript object properties. |
delete object // or delete object.property // or delete object[‘property’] |
JavaScript Relational Operators
JavaScript Relational operators are used to compare its operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result.
Name |
Description |
Syntax |
Example |
The |
|
||
The |
|
JavaScript BigInt Operators
JavaScript BigInt operators support arithmetic operations on BigInt data type, including addition, subtraction, multiplication, division, and exponentiation. Most operators that can be used between numbers can be used between BigInt values as well.
Name |
Description |
Syntax |
Example |
It returns a new BigInt object that represents the original value. |
BigInt(value); |
JavaScript String Operators
JavaScript String Operators include concatenation (+) and concatenation assignment (+=), used to join strings or combine strings with other data types.
Name |
Description |
Syntax |
Example |
It concatenates two string values together, returning another string that is the union of the two operand strings. |
str1 + str2 |
Conclusion
JavaScript operators are essential tools that empower developers to perform a wide array of operations on data, from simple arithmetic to complex logical decisions. By mastering these operators, you can write more efficient, readable, and maintainable code. Whether you’re performing calculations, making comparisons, assigning values, or manipulating data, understanding how to use JavaScript operators effectively is a fundamental skill for any JavaScript developer. As you continue to build your programming expertise, the versatility and power of operators will become increasingly apparent, enabling you to create more dynamic and robust applications.
We have a list of JavaScript Operators reference where you can know more about these operators.