Operators in Javascript (with Example)

In this tutorial, you will learn about different operators in javascript and how to use them with the help of multiple examples.

What is an Operator?

In javascript, operators are the special symbols that are used to perform operations on the operands(values and variables). For example,

Here + is an operator that is performing an addition operation on operands 2 and 3.

Types of Javascript Operators

Here is the list of JavaScript operators that you are going to learn in this tutorial.

Assignment Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
String Operators
Other Operators

Assignment Operators in Javascript

Assignment operators are used to assign values to javascript variables. For example,

Here, the = assignment operator is used to assign 5 to variable x.

Here is the list of some commonly used assignment operators:

OperatorNameExample
=Assignment operatora = 5; // 5
+=Addition assignmenta += 5; // a = a + 5
-=Subtraction assignmenta -= 5; // a = a - 5
*=Multiplication assignmenta *= 5; // a = a * 5
/=Division assignmenta /= 5; // a = a / 5
%=Remainder assignmenta %= 5; // a = a % 5
**=Exponentiation Assignmenta **= 2; // a = a ** 2

Arithmetic Operators in Javascript

Arithmetic operators are used to perform arithmetic operations. For example,

Here the + operator is used to add operands 2 and 3;

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Remainderx % y
++Increment (increments by 1)x++ or ++x
--Decrement (decrements by 1)x-- or --x
**Exponential (Power)x ** y (x^y)

Example 1: Arithmetic operators in JavaScript

let x = 4;
let y = 2;

// addition
console.log(x + y);  // 6

// subtraction
console.log(x - y);  // 2

// multiplication
console.log(x * y);  // 8

// division
console.log(x / y);  // 2

// remainder
console.log(x % y);  // 0

// increment
console.log(++x); // 5
console.log(x++); // prints 5 and then increments by 1
console.log(x);     // 6

// decrement
console.log(--x); // 5
console.log(x--); // prints 5 and then decrements by 4
console.log(x);     // 4

//exponentiation
console.log(x ** y); // 16

Comparison Operators in Javascript

Comparison operator compares two values and as a result, returns either true or false. For example,

let a = 5, b = 4;
console.log( a > b ); // true

Here a and b are the operands and we are using the > comparison operator to compare them.

OperatorDescriptionExample
==Equal to: true if operands are equalx == y
===Strict equal to: true if operands and their types are equalx === y
!=Not equal to: true if operands are not equalx != y
!==Strict not equal to: true if operands are not equal and nor their typesx !== y
>Greater than: true if the left operand is greater than the right operandx > y
<Less than: true if the right operand is greater than the left operandx < y
>=Greater than or equal to: true if the right operand is greater than or equal to the left operandx >= y
<=less than or equal to: true if the right operand is greater than or equal to left operandx <= y

Example 2: Comparison operators in Javascript

// equal to
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal to
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal to
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal to
console.log(2 !== '2'); // true
console.log(2 !== 2); // false

Logical Operators in Javascript

Logical operators are used to perform the logical operation and return boolean values, either true or false. For example,

let x = 2, y = 3;
console.log(x < 3 && y < 4); // true 

Here, && is AND logical operator. It is returning true because both x < 3 and y < 4 are true.

NameDescriptionExample
&&Logical AND: true if both operands are true, else returns falsea && b
||Logical OR: true if either of the operands is true; if both are false, returns false.a || b
!Logical NOT: false if the operand is true and vice versa!a

Example 3: Logical operators in Javascript

// logical AND
console.log( true && true ); // returns true
console.log( true && false ); // returns false

// logical OR
console.log( true || false ); // returns true
console.log( false || false ); // returns false

// logical NOT
console.log( !true ); // returns false
console.log( !false ); // returns true

Bitwise Operators in Javascript

Bitwise operators perform their operations on the binary representation of operands. For example, the binary representation of operand 9 is 1001.

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~ a
<<Left Shifta << b
>>Sign-propagating right shifta >> b
>>>Zero-fill right shifta >>> b

String Operators in Javascript

+ operator in javascript is used with both numbers and strings. If we use + operator with numbers it performs addition and if we use it with strings it performs string concatenation.

Example 4: String operators in Javascript

// string  concatenate
console.log('hello' + 'world');
// helloworld

let x = 'coding';

x += 'claw';  // x = x + 'claw';
console.log(x);
// codingclaw

Output

helloworld
codingclaw

Some other operators in Javascript

Here is the list of some other javascript operators.

OperatorDescriptionExample
,evaluates each of its operands and returns the value of the last operand.let a = ( 1, 2, 3 ); // 3
?:returns values based on a condition( 4 > 2 ) ? ‘yes’ : ‘no’; // yes
deletedeletes an object’s property or an array elementdelete object.property;
typeofreturns a string indicating the type of the unevaluated operand.typeof 2; // “number”
voidspecifies an expression to be evaluated without returning a valuevoid (expression)
inreturns true if the specified property is in the specified objectprop in object
instanceofreturns true if the specified object is of the specified object typeobject instanceof objectType

Leave a Comment

Your email address will not be published. Required fields are marked *