JavaScript Tutorial

Javascript Operators

JavaScript provides a variety of operators that allow you to perform operations on values and variables. Here are some common types of operators in JavaScript:

Unary Operators

These types of operators take only a single operand and perform the operation. The following are the unary operators:-

  • Unary plus (+)  – This type of operand converts an operand into a number.

Input:-

Output:-

Unary minus (-) – This type of operand converts an operand into a number and negate the value after that.

Input:-

Output:-

Increment

prefix/postfix increments (++) – This type of operand adds one to its operand. This increment can happen from both postfix and prefix ways but there effect is different in both ways.

Input:-

Output:-

Decrement

prefix/postfix decrements (--) – This type of operand subtracts one from its operand. This decrement can happen from both postfix and prefix ways but their effect are different in both ways.

Input:-

Output:-

It’s important to note that the prefix increment/ decrement, the value of the variable changed before the statement is evaluated.

The only difference between the postfix and prefix is that JavaScript doesn’t evaluate them until the containing statement has been evaluated.

Arithmetic Operators

JavaScript provides several arithmetic operators for performing mathematical operations on numbers. Here are the common arithmetic operators in JavaScript:

Input:-

Output:-

In the above example, 10 and 20 are added by using the + operator and evaluation to 30. In the same way, we can use the below operators to perform different operations.  

Operator

Name

Description

+

Addition

Used to add operands

-

Subtraction

Used to subtract operands

*

Multiplication

Used to multiply operands

/

Division

Used to divide operands

%

Modulus

Used to get modulus of operands

Note:- In arithmetic, the division of two integers produces a quotient and a remainder. In mathematics, the result of a modulo operation is the remainder of an arithmetic division.

Assignment Operators

An assignment operator assigns a value of the right operand to its left operand. The basic assignment operator is equal (=).

Input:-

Output:-

In the above example, we assigned the number 10 to the variable x. We can also add, subtract or multiply any value on variable x.

Input:-

Output:-

In the above example, the += assignment operator assigns the result of a plus 5 to variable x. The following table illustrates assignment operators that are shorthand for another operator plus the assignment:

Operator

Name

Description

a = b

a = b

Assigns the value of b to a.

a += b

a = a + b

Assigns the result of a plus b to a.

a -= b

a = a - b

Assigns the result of a minus b to a.

a *= b

a = a * b

Assigns the result of a times b to a.

a /= b

a = a / b

Assigns the result of a divided by b to a.

a %= b

a = a % b

Assigns the result of a modulo b to a.

a &=b

a = a & b

Assigns the result of a AND b to a.

a |=b

a =a | b

Assigns the result of a OR b to a.

a ^=b

a = a ^ b

Assigns the result of a XOR b to a.

a <<= b

a = a << b

Assigns the result of a shifted left by b to a.

a >>>= b

a = a >>> b

Assigns the result of a shifted right by b to a.

Logical Operators

These types of operators allow us to compare variables and do something based on the result of that comparison. It includes the logical NOT operator( !), the logical AND operator ( &&), and the logical OR operator ( ||).           

  • Logical NOT operator( !) - JavaScript uses an exclamation point! to represent the logical NOT operator. The! operator can be applied to a single value of any type, not just a Boolean value.

When we apply the! operator to a boolean value, the! returns true if the value is false and returns false if the value is true.

Input:-

Output:-

 

Logical AND operator ( &&) - JavaScript uses the double ampersand (&&) to represent the logical AND operator. The following truth table illustrates the result of the && operator when it is applied to two Boolean values:

A

b

a & b

True

true

true

True

false

false

False

true

false

False

false

false

The result of the && operator is true only if both values are true, otherwise, it is false. For example:

Input:-

[tryjavascript_The result of the && operator is true only if both values are true]

Output:-

  • Logical OR operator (||) - JavaScript uses the double pipe || to represent the logical OR operator. The following truth table illustrates the result of the || operator based on the value of the operands:

A

b

a || b

True

true

true

True

false

true

False

true

true

False

false

false

The || operator returns false if both values evaluate to false. In case either value is, the || operator returns true. For example:

Input:-

Output:-

Operator Precedence

When we use the mixed logical operators in an expression, the JavaScript engine evaluates the operators based on a specified order, and this order is called the operator precedence. In other words, the operator precedence is the order that an operator is executed.

The precedence of the logical operator is in the following order from the highest to lowest:

  • Logical NOT (!)
  • Logical AND (&&)
  • Logical OR (||)

Comparison Operators

The JavaScript comparison operator compares the two operands. The following table illustrates the JavaScript comparison operators:

Operator

Description

Example

==

Is equal to

10==20 = false

===

Identical (equal and of the same type)

10==20 = false

!=

Not equal to

10!=20 = true

!==

Not Identical

20!==20 = false

>

Greater than

20>10 = true

>=

Greater than or equal to

20>=10 = true

<

Less than

20<10 = false

<=

Less than or equal to

20<=10 = false

==

Is equal to

10==20 = false

As we are comparing numbers in the above example, we can also compare strings. It compares the character codes numerically one by one in the string.

Input:-

Output:-

We can compare the object with an object, Boolean with a number, NaN with other values, a number with a value of another type, and object with a non-object. Following are some examples and their output in brief.

Bitwise Operators

The bitwise operators perform bitwise operations on operands. The bitwise operators are as follows:

Operator

Description

Example

&

Bitwise AND

(10==20 & 20==33) = false

|

Bitwise OR

(10==20 | 20==33) = false

^

Bitwise XOR

(10==20 ^ 20==33) = false

~

Bitwise NOT

(~10) = -10

<<

Bitwise Left Shift

(10<<2) = 40

>>

Bitwise Right Shift

(10>>2) = 2

>>>

Bitwise Right Shift with Zero

(10>>>2) = 2

&

Bitwise AND

(10==20 & 20==33) = false

|

Bitwise OR

(10==20 | 20==33) = false

Go back to Previous Course