Open in App
Not now

# JavaScript Operators

• Difficulty Level : Basic
• Last Updated : 30 Mar, 2023

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.

```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) ` ` `  `// Increament Operator ` `var` `g = 2; ` `g1 = g++; ` `console.log(g) ` ` `  `// Decrement Operator ` `var` `h = 2; ` `h1 = h--; ` `console.log(h) ` ` `  `// Unary plus Operator ` `var` `i = 3; ` `i1 = +i; ` `console.log(i1) ` ` `  `// Negation Operator ` `var` `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`
`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 `
`Y *= A is equivalent to Y = Y * A`
`Y /= A is equivalent to Y = Y / A`
`Y %= A is equivalent to Y = Y % A`
`Y **= A is equivalent to Y=Y ** A`
`Y <<= A is equivalent to Y=Y << A`
`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`
` Y ^= A is equivalent to Y= Y ^ A`

Example: In this example, we will use all assignment operators.

## Javascript

 ` `

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. `
`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

 ` `

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

 ` `

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```
```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

 ` `

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

 ``

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.

My Personal Notes arrow_drop_up
Related Articles