in , ,

JavaScript Operators

JavaScript Operators
JavaScript Operators

In this tutorial, you will learn about various operators available in JavaScript and how to use them with the help of examples.

JavaScript incorporates operators as in different languages. An operator plays out some procedure on single or various operands (data value) and produces a result. For instance 1 + 2, where + sign is an operator and 1 is the left operand and 2 is the right operand. + operator adds two numeric values and produces a result which is 3 for this situation.

In this article, you will learn-

What is an Operator?

In JavaScript, an operator is a special symbol used to perform procedures on operands (values and variables). For instance,

2 + 3; // 5

Here + is an operator that performs addition, and 2 and 3 are operands.


JavaScript Operator Types

Here is a list of different operators you will learn in this tutorial.

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

JavaScript Assignment Operators

Assignment operators are used to assign values to variables. For instance,

const x = 5;

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

Here’s a list of ordinarily used assignment operators:

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

Note: The usually used assignment operator is =. You will comprehend other assignment operators, for example, +=, – =, *=, and so on once we learn arithmetic operators.


JavaScript Arithmetic Operators

Arithmetic operators are used to performing arithmetic calculations. For example,

const number = 3 + 5; // 8

Here, the + operator is used to add two operands.

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–
**Exponentiation (Power)x ** y

Example 1: Arithmetic operators in JavaScript

let x = 5;
let y = 3;

// addition
console.log('x + y = ', x + y);

// subtraction
console.log('x - y = ', x - y);

// multiplication
console.log('x * y = ', x * y);

// division
console.log('x / y = ', x / y);

// remainder
console.log('x % y = ', x % y);

// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // x returns 6 and then increases by 1
console.log('x = ', x);

// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // x returns 6 and then increases by 1
console.log('x = ', x);

//exponentiation
console.log('x ** y =', x ** y);

Output

x + y =  8
x - y =  2
x * y =  15
x / y = 1.6666666666666667
x % y = 2
++x =  6
x++ = 6
x = 7
--x = 6
x-- = 6
x = 5
x ** y = 125

Note: The ** operator was introduced in EcmaScript 2016 and some browsers may not support them. To learn more, visit JavaScript exponentiation browser support.


JavaScript Comparison Operators

Comparison operator compares at two values and returns a boolean worth, either true or false. For instance,

const a = 3, b = 2;
console.log(a > b); // true 

Here, the comparison operator > is used to compare whether a is greater than b.

OperatorDescriptionExample
==Equal to: returns true if the operands are equalx == y
!=Not equal to: returns true if the operands are not equalx != y
===Strict equal to: true if the operands are equal and of the same typex === y
!==Strict not equal to: true if the operands are equal but of different type or not equal at allx !== y
Greater than: true if left operand is greater than the right operandx > y
>=Greater than or equal to: true if left operand is greater than or equal to the right operandx >= y
Less than: true if the left operand is less than the right operandx < y
<=Less than or equal to: true if the left operand is less than or equal to the right operandx <= y

Example 2: Comparison operators in JavaScript

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

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

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

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

Output

true
true
true
true
true
false
false
true

Comparison operators are used in decision making and loops. You will learn about the use of the comparison operators in detail in later tutorials.


JavaScript Logical Operators

Logical operators perform legitimate activities and return a boolean worth, either true or false. For instance,

const x = 5, y = 3;
(x < 6) && (y < 5); // true

Here, && is the logical operator AND. Since both x < 6 and y < 5 are true, the result is true.

OperatorDescriptionExample
&&Logical AND: true if both the operands are true, else returns falsex && y
||Logical OR: true if either of the operands is true; returns false if both are falsex || y
!Logical NOT: true if the operand is false and vice-versa.!x

Example 3: Logical Operators in JavaScript

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

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

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

Output

true
false
true
false

Logical operators are used in decision making and loops. You will learn about the use of logical operators in detail in later tutorials.


JavaScript Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<< Left shift
>> Sign-propagating right shift
>>> Zero-fill right shift

JavaScript String Operators

In JavaScript, you can likewise use the + operator to connect (join) at least two strings.

Example 4: String operators in JavaScript

// concatenation operator
console.log('hello' + 'world');

let a = 'JavaScript';

a += ' tutorial';  // a = a + ' tutorial';
console.log(a);

Output

helloworld
JavaScript tutorial

Note: When + is used with strings, it performs connection. However, when + is used with numbers, it performs addition.


Other JavaScript Operators

Here’s a list of different operators available in JavaScript. You will learn about these operators in later tutorials.

OperatorDescriptionExample
,evaluates multiple operands and returns the value of the last operand.let a = (1, 3 , 4); // 4
?:returns value based on the condition(5 > 3) ? ‘success’ : ‘error’; // “success”
deletedeletes an object’s property, or an element of an arraydelete x
typeofreturns a string indicating the data typetypeof 3; // “number”
voiddiscards the expression’s return valuevoid(x)
inreturns true if the specified property is in the objectprop in object
instanceofreturns true if the specified object is of of the specified object typeobject instanceof object_type

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

JavaScript Data Types

JavaScript Data Types

JavaScript Type Conversions

JavaScript Type Conversions