Loading...
Loading...
00:00:00

Operators in Javascript

In JavaScript, operators are used to perform actions on values. There are several types of operators:

  1. Arithmetic operators: Used to perform arithmetic operations on numeric values. Examples include + (addition), - (subtraction), * (multiplication), / (division), and % (modulus).

  2. Assignment operators: Used to assign values to variables. Examples include = (simple assignment), += (addition assignment), -= (subtraction assignment), *= (multiplication assignment), /= (division assignment), and %= (modulus assignment).

  3. Comparison operators: Used to compare values and return a Boolean value (true or false). Examples include == (equality), != (inequality), === (strict equality), !== (strict inequality), > (greater than), >= (greater than or equal to), < (less than), and <= (less than or equal to).

  4. Logical operators: Used to perform logical operations on Boolean values. Examples include && (logical AND), || (logical OR), and ! (logical NOT).

  5. Bitwise operators: Used to perform bitwise operations on binary values. Examples include & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift).

  6. Unary operators: Used to operate on a single operand. Examples include + (unary plus), - (unary minus), ++ (increment), -- (decrement), and typeof (returns a string indicating the type of the operand).

  7. Ternary operator: Also known as the conditional operator, used to assign a value to a variable based on a condition. Example syntax: condition ? value1 : value2

Understanding operators is an important part of writing effective JavaScript code, as they allow you to manipulate values and perform logical operations.

Arithmetic Operators

Arithmetic operators performs Arithmetic operation or calculations on numbers. such as addition, multiplication, division, modulus, increment,decrement etc.

String Concatenation Operator

This operator can be used as adding two strings. Using this operator we can add two strings or concatenate strings. We can add number and string together and final result will be string. Here + operator is called concatenation operator.

let x = 10;
let y = 'is number';
let z = x+y;  // output will be '10is number' 

+ Plus Operator

This operator is used to add numbers values. + is binary operator, it require two operands to perform the operation.

- Minus Operator

This operator is used to subtract numbers values. - is binary operator, it require two operands to perform the operation.

* Multiply Operator

This operator is used to multiply numbers values. * is binary operator, it require two operands to perform the operation.

/ Division Operator

This operator is used to divide numbers values. / is binary operator, it require two operands to perform the operation.

let x = 10;
let y = 20;

console.log(x+y);
console.log(x-y);
console.log(x*y);
console.log(x/y);

% Modulus Operator

This operator returns remainder after division. % is binary operator, it require two operands to perform the operation.

let x = 10;
let y = 3;
console.log(x%y);  //divide two variables 

** Exponentiation Operator

This operator is used to find exponent of x to the power y xy. This operator require two operands to perform the operation.

let x = 10;
let y = 5;
console.log(x**y);  //return the exponent of x to the power y

++ Increment Operator

This operator is increment the value by 1.

++ is unary operator, it require only one operand to perform the operation. It has two types

Pre-increment : pre-increment operator first increment the value by 1 and then return it.
Post-increment : post-increment operator is first return the value and then increment by 1.
let x = 10;
let y = x++;  // post-increment 
let z = ++x;  // pre-increment 
console.log(y);
console.log(z);

-- Decrement Operator

This operator is decrement the value by 1.

-- is unary operator, it require only one operand to perform the operation. It has two types

Pre-decrement: pre-decrement operator is first decrement the value by 1 and then return it.
Post-decrement: post-decrement operator is first return the value and then decrement by 1.
let x = 10;
let y = x--;  // post-decrement 
let z = --x;  // pre-decrement 
console.log(y);
console.log(z);
 

= Assignment Operator

Assignment operator is used to assign the value in variable.

Some Assignment Operators

These Assignment operators are used to assign the value in variable with some operation.

operators operations meaning
= a=b a=b
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b
**= a**=b a=a**b
 

Comparison Operators

These comparison operators are used to compare two variables value or its types.

operators descriptions comparing return value
== equal to 10 == 20 false
=== equal variable value and its type "20" === 20 false
!= not equal "20" != 20 false
!== not equal variable value and its type "20" !== 20 true
< less than 20 < 100 true
> greater than 20 > 100, "100">"20", 20>20 false
>= greater than or equal 20 >= 20 , 20 > = 10 true
<= less than or equal 20<=20 , 20 <=100 true
? : ternary or conditional oprator 20>10? yes : no yes
 

Logical Operators

These logical operators are used to determine the logic between variables or values. logical operators always returns boolean values.

operators operations Examples output
&& and 10 > 20 && 10 < 5 false
|| or 10 > 20 || 10 < 5 false
! not !(10 > 20 || 10 < 5) true
 

Bitwise Operators

These bitwise operators works on 32 bit binary numbers.

Operator Name description Examples meaning Output Decimal equal
& AND Sets each bit to 1 if both bits are 1 5 & 1 0101 & 0001 0001 1
| OR Sets each bit to 1 if one of two bits is 1 5 | 1 0101 | 0001 0101 5
~ NOT Inverts all the bits ~ 5 ~0101 1010 10
^ XOR Sets each bit to 1 if only one of two bits is 1 5 ^ 1 0101 ^ 0001 0100 4
<< Zero fill left shift Shifts left by pushing zeros in from the right and let the leftmost bits fall off 5 << 1 0101 << 1 1010 10
>> Signed right shift Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off 5 >> 1 0101 >> 1 0010 2
>>> Zero fill right shift Shifts right by pushing zeros in from the left, and let the rightmost bits fall off 5 >>> 1 0101 >>> 1 0010 2

Conditional Operator (Ternary Operator)

conditional operator is also known as ternary operator because it has three operands to perform the operations. ' ? : ' is called ternary operator. First operand should be condition and if the first condition is true then execute the second operand if first condition is false then execute the third operand. we can use Ternary operator like if statement.
Syntax is Condition ? True : False ; 

let x = 10;
let result = x<20 ? "x is less then 20": "x is not less then 20";
console.log(result);

 

var x = 20;   //assign 20 in x 

Operator Precedence Rules

In JavaScript, operators have a specific order of precedence, which determines the order in which they are evaluated in an expression. The order of precedence is as follows:

  1. Grouping: ( )

  2. Unary: +, -, ++, --, typeof, !

  3. Multiplication and division: *, /, %

  4. Addition and subtraction: +, -

  5. Relational: <, <=, >, >=, instanceof, in

  6. Equality: ==, !=, ===, !==

  7. Logical AND: &&

  8. Logical OR: ||

  9. Conditional: ?:

  10. Assignment: =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

Note that operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, their evaluation order depends on their associativity, which can be either left-to-right or right-to-left.

It's important to understand operator precedence rules in JavaScript, as they can affect the outcome of an expression. To avoid confusion and ensure correct evaluation, it's recommended to use parentheses to explicitly define the order of operations in complex expressions.

Precedence Rules

precedence rule decides the which operator should be perform the operation first. This is like BODMAS in mathematics but BODMAS rule not allowed here. we have to use this operator precedence rules.

Precedence Value Operators Usages Example
21 ( ) Expression grouping (a+10)
20 . Member object.value
20 [] Member object["value"]
20 () Function call function_name()
20 new Create object new Date()
18 ++ Postfix Increment i++
18 -- Postfix Decrement i--
17 ++ Prefix Increment ++i
17 -- Prefix Decrement --i
17 ! Logical not !(a==b)
17 typeof Checking Type typeof a
16 ** Exponentiation 5** 2
15 * Multiplication 5 * 2
15 / Division 5 / 2
15 % Modulus 5 % 2
14 + Addition 5 + 2
14 - Subtraction 5 - 2
13 << Shift left a << 2
13 >> Shift right a >> 2
13 >>> Shift right (unsigned) a >>> 2
12 < Less than x < y
12 <= Less than or equal a <= b
12 > Greater than a > b
12 >= Greater than or equal a >= b
11 == Equal a == b
11 === Strict equal a === b
11 != not equal a != b
11 !== Strict not equal a !== b
10 & Bitwise AND a & b
9 ^ Bitwise XOR a ^ b
8 | Bitwise OR a | b
7 && Logical AND a && b
6 || Logical OR a || b
5 ?? Nullish Coalescing a ?? b
4 ? : Ternary a>b ? "true":"false"
3 += Assignment a += b
3 /= Assignment a /= b
3 -= Assignment a -= b
3 *= Assignment a *= b
3 %= Assignment a %= b
3 <<= Assignment a <<= b
3 >>= Assignment a >>= b
3 >>>= Assignment a >>>= b
3 &= Assignment a &= b
3 ^= Assignment a ^= b
3 |= Assignment a |= b
2 yield Pause Function yield a
1 , seperator a , b