JavaScript Operators
JavaScript operators operate the operands, these are symbols that are used to manipulate a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used to compare values, perform arithmetic operations, etc.
JavaScript Operators: There are various operators supported by JavaScript.
- JS Arithmetic Operators
- JS Assignment Operators
- JS Comparison Operators
- JS Logical Operators
- JS Ternary Operators
- JS Bitwise Operators
- JS typeof Operator
JavaScript Arithmetic Operators: These are the operators that operate upon the numerical values and return a numerical value.
- Addition (+): Addition ‘+’ operator performs addition on two operands. This ‘+’ operator can also be used to concatenate (add) strings.
Y = 5 + 5 gives Y = 10 Y = "Geeks" + "for" + "Geeks" gives Y = "GeeksforGeeks" Y = "Geeks" + 4 + "Geeks" gives Y = "Geeks4Geeks"
- Subtraction (-): Subtraction ‘-‘ operator performs subtraction on two operands.
Y = 5 - 3 gives Y = 2
- Multiplication (*): Multiplication ‘*’ operator performs multiplication on two operands.
Y = 5 * 5 gives Y = 25
- Division (/): Division ‘/’ operator performs division on two operands (divide the numerator by the denominator).
Y = 5 / 5 gives Y = 1
- Modulus (%): Modulus ‘%’ operator gives a remainder of an integer division.
A % B means remainder (A/B) Y = 5 % 4 gives Y = 1
- Exponentiation (**): Exponentiation ‘**’ operator give the power of the first operator raised to the second operator.
Y = 5 ** 3 gives Y = 125
- Increment(++): Increment ‘+ +’ operator increases an integer value by one.
let A = 10 and Y = A + + then A = 11, Y=10 if A = 10 and Y = + + A then A = 11, Y=11
- Decrement (- -): Decrement ‘- -‘ operator decreases an integer value by one.
let A = 10 and Y = A - - then A = 9, Y=10 if A = 10 and Y = - - A then A = 9, Y=9
- Unary (+): Unary ‘+’ is the fastest and preferred way of converting something into a number
+a means a is a positive number
- Negation (-): Negation ‘-‘ operator gives the negation of an operand.
-a means a is a negative number
Example: In this example we will use all assignment operators.
Javascript
// Addition Operator let a = 1 + 2 console.log(a); // Subtraction Operator let b = 10 - 7 console.log(b); // Multiplication Operator let c = 3 * 3 console.log(c); // Division Operator let d = 1.0 / 2.0 console.log(d); // Modulas Operator let e = 9 % 5 console.log(e) // Exponentian Operator let f = 2 ** 5 console.log(f) // Increment Operator let g = 2; g1 = g++; console.log(g) // Decrement Operator let h = 2; h1 = h--; console.log(h) // Unary plus Operator let i = 3; i1 = +i; console.log(i1) // Negation Operator let j = 3; j1 = -j; console.log(j1) |
Output:
3 3 9 0.5 4 32 3 1 3 -3
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
- Assignment (=): This operator assigns the right operand value to the left operand.
If A = 10 and Y = A then Y = 10
- Addition Assignment (+=): Sums up left and right operand values and then assigns the result to the left operand.;
Y += 1 gives Y = Y + 1
- Subtraction Assignment (- =): 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
- Multiplication Assignment (*=): 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
- Division Assignment (/ =): 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
- Modules/Remainder Assignment (% =): 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
- Exponentiation Assignment (** =): This raises the value of a variable to the power of the right operand.
Y **= A is equivalent to Y=Y ** A
- Left Shift Assignment (<< =): 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
- Right Shift Assignment (>> =): 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
- Bitwise AND Assignment (& =): It does a bitwise AND operation on the operand, and assigns the result to the variable.
Y &= b is equivalent to Y = Y & A
- Bitwise OR Assignment (| =): It does a bitwise OR operation on the operand, and assigns the result to the variable.
Y |= A is equivalent to Y= Y | b
- Bitwise XOR Assignment (^ =): It does a bitwise XOR operation on the operand, and assigns the result to the variable.
Y ^= A is equivalent to Y= Y ^ A
Example: In this example, we will use all assignment operators.
Javascript
<script> // Assignment Operator let a = 2; console.log(a); // Addition Assignment Operator const b= 3; console.log(a = b + 1); // Subtraction Assignment Operator let yoo=4; console.log(foo=yoo-1); // Multiplication Assignment console.log(yoo=yoo*2); // Division Assignment Operator const moo=2; console.log(yoo=yoo/moo); // Modulas Assignment Operator console.log(yoo%=2); // Exponentian Assignment Operator console.log(yoo**moo); // Left Shift Assignment console.log(yoo<<=2); // Right Shift Assignment console.log(yoo>>=2); // Bitwise AND Assignment console.log(yoo&=2); // Bitwise OR Assignment console.log(yoo|=2); // Bitwise XOR Assignment console.log(yoo^=2); </script> |
Output:
2 4 3 8 4 0 0 0 0 0 2 0
JavaScript Comparison Operators: Comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values.
- Equality (==): Compares the equality of two operands. If equal then the condition is true otherwise false.
Y = 5 and X = 6 Y = = X is false.
- Strict equality (===): Compares the equality of two operands. If equal then the condition is true otherwise false.
Y = 5 and X = '5' Y = = = X is false.
- Inequality (!=): compare the equality of two operands with type. If both value and type are equal then the condition is true otherwise false.
let X = 10 then X ! = 11 is true.
- Strict inequality(!==): Compares inequality of two operands. True if operands are not equal.
let X = 10 then X ! == 11 is true.
- Greater than (>): 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.
- Less than (<): 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.
- Greater than or Equal to (> =): 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.
- Less than or Equal to (<= ): 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.
Example: In this example, we will use all comparison operators.
Javascript
<script> // Assigning values let val1 = 5; let val2 = 5; // Equality Operator console.log(val1 == val2); // Strict equality Operator console.log(val1 === val2); // Inequality Operator console.log(val1 != val2); // Strict Inequality Operator console.log(val1 !== val2); // Greater than Operator console.log(val1 > val2); // Greater than or equal Operator console.log(val1 >= val2); // Less than Operator console.log(val1 < val2); // Less than or equal Operator console.log(val1 <= val2); </script> |
Output:
true true false false false true false true
JavaScript Logical Operators: These operators are used to determine the logic between variables or values.
- Logical AND (&&): 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.
- Logical OR (||): 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.
- Logical NOT (!): It reverses the boolean result of the operand (or condition).
Y = 5 and X = 0 !(Y || X) is false.
Example: In this example, we will use all logical operators.
Javascript
<script> // Assigning values let val1 = 5; let val2 = 5; // Logical !(NOT) Operator console.log(!val2); // Logical &&(AND) Operator console.log( val1 && val2 ); // Lgical ||(OR) Operator console.log( val1 || val2 ); </script> |
Output:
false 5 5
JavaScript Bitwise Operator: 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.
- Bitwise AND(&): The operator returns true only if both the operands are true
A = 6, B=1 A&B = 0
- Bitwise OR(|): The operator returns true even if one of the operands is true
A = 6, B=1 A|B = 7
- Bitwise XOR(^): The operator returns true if both operators are distinct
A = 6, B=1 A^B = 7
- Bitwise NOT(~): This operator is used to invert the boolean value of the operand
A = 6 ~A = -7
- Bitwise Left Shift(<<): 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
- Bitwise Right Shift(>>): 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
- Zero Fill Right Shift(>>>): It is the same as a bitwise right shift the only difference is that overflowing bits are discarded.
A = 6, B=1 A>>>B = 3
JavaScript Ternary Operator: The ternary operator has three operands. It is the simplified operator of if/else.
Ternary Operator(?): 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
Example: In this example, we will use the ternary operator.
Javascript
<script> // Assigning values let PMarks = 40 // Ternary Operator let result = (PMarks > 39)? "Pass" : "Fail" ; console.log(result); </script> |
Output:
Pass
JavaScript typeof Operator: It returns the data type of its operand in the form of a string. The operand can be any object, function, or variable.
JavaScript typeof: It returns the operand type, The possible types that exist in javascript are undefined, Object, boolean, number, string, symbol, and function.
typeof variable;
Example: In this example, we will use typeof operator.
javascript
<script type= "text/javascript" > let a = 17; let b = "GeeksforGeeks" ; let c = "" ; let d = null ; console.log( "Type of a = " + ( typeof a)); console.log( "Type of b = " + ( typeof b)); console.log( "Type of c = " + ( typeof c)); console.log( "Type of d = " + ( typeof d)); console.log( "Type of e = " + ( typeof e)); </script> |
Output:
Type of a = number Type of b = string Type of c = string Type of d = object Type of e = undefined
We have a list of JavaScript Operators reference where you can know more about these operators.
Please Login to comment...