You are currently viewing JavaScript Operators

JavaScript Operators

JavaScript Operators are the symbol which is used to perform operations on operands.

Operands can be values and variable.

10 + 20 = 30 // 10 and 20 are operands and + is operator

10 * 20 = 200 // 10 and 20 are operands and * is operator

20 - 10 = 10 // 10 and 20 are operands and - is operator

20 / 10 = 2 // 10 and 20 are operands and / is operator

Types of JavaScript Operators

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • String Operators
  • Assignment Operators
  • Bitwise Operators
  • Conditional (or ternary) Operators

Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operations between operands.

OperatorOperator NameExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
**Exponentiationa ** b
++Increment++a and a–
Decrement–a and a–
let a = 20;
let b = 10;

// Addition
console.log('a + b = ', a + b); 
 // 30

// Subtraction
console.log('a - b = ', a - b); 
 // 10

// Multiplication
console.log('a * b = ', a * b); 
 // 200

// Division
console.log('a / b = ', a / b);
  // 2

// Remainder
console.log('a % b = ', a % b);  
 // 0

Comparison Operators

Comparison Operator are used for comparison between two operands.

OperatorOperator NameExample
==Equal to a == b
!=Not Equal toa != b
>Greater thena > b
>=Greater then or equal to a >= b
<Less thena < b
<=Less then or equal toa <= b
===Strictly equal (is exactly equal to )a === b
// Equal operator
console.log(0 == 0); // true
console.log(12 == '2'); // false

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

// strict equal operator
console.log(0 === 0); // true
console.log(22 === '22'); // false

Logical Operators

Logical Operator are used to perform logical operation and it return a Boolean value (true , false).

OperatorOperator NameExample
&&Logical ANDa && b
||Logical ORa || b
!Logical NOT!a
let a = 1, b = 2;
(a < 2) && (b > 1); // true

String Operators

The + operator, and the += operator is used to concatenate strings.

OperatorOperator NameExample
+used for concatenate Stringlet t = “t1” + “t2”
+=used for concatenate Stringlet t += “t1”
// concatenation operator
console.log('Code' + 'Shruta');let a =0
let b=1
console.log(a & b);//0
console.log(a | b);//1
console.log(a ^ b);//1
console.log(~a);//-1
console.log(b >> 1);//0
console.log(b << 1)//2
let a = 'Hello ';
a += ' World';  
console.log(a);

Assignment Operators

Assignment Operators are used to assign values to variables.

OperatorOperator NameExample
=Assignment Operatora = 2
+=Addition Assignmenta += 2
-=Subtraction Assignmenta -= 2
*=Multiplication Assignmenta *= 2
/=Division Assignmenta /= 2
%=Modulus Assignmenta %= 2
**=Exponentiation Assignmenta **= 2
//Addition Assignment
let x = 100;
x += 15;
console.log(x);//115

//Subtraction Assignment
let y = 100;
y -= 15;
console.log(y);//85

//Multiplication Assignment
let z = 100;
z *= 15;
console.log(z);//1500

Bitwise Operators

Bitwise Operators perform operations on binary values.

OperatorOperator NameExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~ a
>>Right Shifta >> 1
<<Left Shifta << 1
let a =0
let b=1

//Bitwise AND
console.log(a & b);//0

//Bitwise OR
console.log(a | b);//1

//Bitwise XOR
console.log(a ^ b);//1

//Bitwise NOT
console.log(~a);//-1

//Right Shift
console.log(b >> 1);//0

//Left Shift
console.log(b << 1)//2

Conditional Operators

It assign a value to a variable based on some condition.

SyntaxDescription Example
variable_name=(condition) ?value1:value2 If condition true then value1 will be assigned to variable and if condition is false then value2 will be assigned.a= (4>5)? 4:5
so a value is 5 here because 4>5 will return false.

!!JavaScript Operators !!.

Check All JavaScript Tutorial here JAVASCRIPT TUTORIAL.

Written By : Ankita Kataria.

This Post Has One Comment

Leave a Reply