Home >>Javascript Tutorial >JavaScript Operators

JavaScript Operators

Javascript Operators

In Javascript, words and symbols in expressions that perform operations certain values to arrive at another value are known as operators.

Any value on which an operator performs some action is called an operand.
An expression may contain one operand and one operator (called a unary operator) or two operands separated by one operator (called a binary operator). Many of the same symbols are used in a variety of operators.

 

The combination and order of those operator symbols are what distinguish their powers. In simple words, an operator is the one operating the operand.
There are many use of operators in Javascript such as comparing values, performing arithmetic operations, etc.

These are some of the JavaScript operators:

  • Arithmetic operators
  • Comparison operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Ternary Operators

Arithmetic Operators:

These operators are used to operate mathematical operations between numeric operands.

Operator Description
+ Used to add two numeric operands.
- Used to subtract right operand from left operand
* Used to multiply two numeric operands.
/ Used to divide left operand by right operand.
% Modulus operator. It returns remainder of two operands.
++ Increment operator. It Increases operand value by one.
-- Decrement operator. It decreases value by one.

The following example will help you in understand the Arithmetic Operators in running different tasks on Javascript operators:

var a = 2, b = 4;
a + b; //returns 6
b - a; //returns 2
a * b; //returns 8
b / a; //returns 2
a % 2; //returns 1
a++; //returns 3
a--; //returns 1

Note : + Operator executes a series of operations provided that one of the operands is of string type. This example will help you understand the working of + Operator on operands of various data types.

var w = 5, x = "Hello ", y = "World!", z = 10;
w + x; // "5Hello "
x + y; // "Hello World!"
w + z; // 15

Comparison Operators

Any time you compare two values in JavaScript, the result is a Boolean true or false value.
You have a wide selection of comparison operators to choose from, depending on the kind of test you want to apply to the two operands.
There are basically 4 ways by which the equality is tested that are listed as follows:

  • '==' operator
  • '===' operator
  • SameValuezero: this is mainly used in maps, arrays and sets.
  • SameValue: these are used elsewhere.

All the various types of comparison operators are mentioned in the table below:

Operator Description Example
== Is equal to 11==22 = false
=== Identical (equal and of same type) 11==22 = false
!= Not equal to 10!=20 = true
!== Not Identical 22!==22 = false
> Greater than 22>12 = true
>= Greater than or equal to 22>=12 = true
< Less than 22<12 = false
<= Less than or equal to 22<=12 = false

'==' operator:

In javascript, the use of this operator is mainly in comparing two value on both the sides and then return true or false as a result. This javascript operator, evaluates the equality only after converting or we can say bringing both the values to a common type known as type coercion.

For numeric values, the results are the same as those you would expect from your high school algebra class.

10 == 10            // true 
10 == 10.0          // true 
9 != 10             // true 
9 > 10              // false 
9.99 <= 9.98        // false

'===' operator:

This JavaScript operator is also called as strict equality operator, it simultaneously compares the value and the type.

JavaScript Bitwise Operators

The bitwise operators carryout bitwise operations on the operands. Here are the bitwise operators:

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

JavaScript Logical Operators

All the various types of Logical operators are mentioned in the table below:

Symbol Description Example
&& Logical AND (12==22 && 22==33) = false
|| Logical OR (12==22 || 22==33) = false
! Logical Not !(12==22) = true

JavaScript Assignment Operators

All the various types of Assignment operators are mentioned in the table below:

Symbol Description Example
= Assign 10+10 = 20
+= Add and Assign var a=10; a+=20; Now a = 30
-= Subtract and assign var a=20; a-=10; Now a = 10
*= Multiply and assign var a=10; a*=20; Now a = 200
/= Divide and assign var a=10; a/=2; Now a = 5
%= Modulus and assign var a=10; a%=2; Now a = 0

Ternary(Special) Operators

Symbol Description
(?:) Conditional Operator returns value based on the condition. It is like if-else.

No Sidebar ads