Operators are important components of different programming languages. They are what programmers use to change the functions of statements or expressions in JavaScript. You can use an operator to manipulate the value or operand of a variable.
Again, you can use operators to perform specific mathematical and logical computations on operands. Again, you can use operators to compare values, perform arithmetic operations, etc.
The following operators are supported by JavaScript:
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Ternary Operators
typeof Operator
Read on to learn more about JavaScript operators.
1. Arithmetic Operators
Arithmetic operators are needed to solve mathematical problems or perform computations. There are many of these operators, and they are exactly the operators you see and use in solving real-world problems in maths or other calculative subjects.
Arithmetic Operators | description | examples |
Addition (+) | In JavaScript, the “+” operator is used to sum numbers. Once more, it is employed to concatenate or join strings. | Y = “they ” + “said ” + “no to his” gives Y = “demand” |
Subtraction(-) | To subtract a specific number from a parent number, use the “-“ operator. For instance, | Y = 4 – 2 results in Y = 2. |
Multiplication(*) | The “*” operator multiplies two operands; for instance | Y = 7 * 7 yields Y = 49. |
Division(/) | The “/” operator divides two numbers. For instance, | Y = 60 / 10 results in y = 6. |
+ + (Increment) | The “+ +” operator raises an integer value by one. For instance, if | A = 2 and Y = A + +, A = 3 |
– – (Decrement) | When used, the ‘- -‘ operator reduces an integer value by one. For instance, if | A = 3 and Y = A – -, A = 2 |
Modulus (%) | The “%” operator returns the remainder of an integer division; for instance, | Y = 11% 3 returns Y = 2 |
2. Assignment Operators
You can use assignment operators to accomplish the task of assigning different values to variables. We use “=” to assign values, “+=” sums up left and right operand values, and then assigns the result to the left operand.
The “-=” operator divides the difference between the right and left side values, and then it assigns the outcome to the left operand. Y = Y – 1 is the result of Y -= 1. Same as *=, /= l, %=.
3. Comparison Operators
Comparison operators in JavaScript are used to compare two variables. Examples of comparison operators include:
== | This operator can be used to assess the equality of two operands. |
=== | This operator allows you to check whether two operands are of the same data type and have the same value. |
!= (Not Equal) | This operator is best for measuring two operands’ inequality. |
> (Greater than) | This operator determines whether the value on the left exceeds the value on the right. |
< (Less than) | With this operator, you can check whether the value on the left is lower than the value on the right. |
> = (Greater than or Equal to) | If the left side operand is greater than the right side operand or equal to it, the operator evaluates the condition. |
<= (Less than or Equal to) | The value of the right side operand must be greater than or equal to the value of the left side operand for this operator to be valid. |
4. Logical Operators
Logical operators are designed for conditional operations whose output is either true or false after accessing a logical expression. AND (&&), OR (||), and Not (!) are common logical operators.
5. Ternary Operator
The conditional (ternary) operator takes three operands; a condition, an expression to be executed if the condition is true, and an expression to be executed if the condition is false. It is like the short form of the if-else condition.
Syntax:
Y =? A : B
6. typeof Operator
The typeof operator has a single operand that can be of any type, making it a unary operator. Its value is a string that contains the operand’s data type information. When passing parameters to a function, the typeof operator is used to determine whether or not they are integers.
The use of each of these operands is demonstrated in the code below.
// the code below prints the type of data coming after the typeof operator
console.log(typeof true)
console.log(typeof -67)
console.log(typeof “nike”)
console.log(typeof 3.455)
console.log(typeof Infinity)
#Output
Boolean
Number
String
Number
Number
6. JavaScript Bitwise Operators
The bitwise operator in JavaScript operates on the bits (i.e., 0s and 1s). It does a bit-by-bit operation on the converted binary equivalent numbers, converts the input decimal numbers to their corresponding binary numbers, and then returns the result as a decimal number.
Bitwise xor (^) |
Bitwise NOT (~) |
Bitwise AND (&) |
Bitwise OR (|) |
Bitwise Right Shift (>>) |
Bitwise Right Shift with Zero (>>>) |
Bitwise Left Shift (<<) |
Conclusion
We introduced, explained, and provided examples of JavaScript operators in this article. In the next article, we will look at how to use popular operators like arithmetic, comparison, and assignment.