Operators in JavaScript.

Operators in JavaScript

In this article, we will learn about Operators in JavaScript.

What is an Operator?

An operator is a special symbol in JavaScript that is used to perform operations on operands (values and variables). For example:

4 + 5; // 9

Here, operator + performs addition, and 4 and 5 are operands.

JavaScript Operator Types

The following is a list of the various operators that we will learn in this tutorial.

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

Assignment Operators in JavaScript

Variables are assigned values using the assignment operators. For example:

const a = 7;

The = operator is used to assign the value 5 to variable x in this case.

Here’s a list of the most common assignment operators:

OperatorNameExample
=Assignment operatorx = 3;
+=Addition assignmentx += 3; // x = x + 3
-=Subtraction Assignment x -= 3; // x = x – 3
*=Multiplication Assignment x *= 3; // x = x * 3
/=Division Assignment x /= 3; // x = x / 3
%=Remainder Assignment x %= 3; // x = x % 3
**=Exponentiation Assignment x **= 3; // x = x ** 3

Note: The assignment operator = is commonly used. Once you’ve learned arithmetic operators, you’ll be able to understand other assignment operators like +=, -=, *=, and so on.

Arithmetic Operators in JavaScript

To perform arithmetic calculations, arithmetic operators are used. For example:

const number = 7 + 5; // 12

The + operator is used to combine two operands in this case.

Here’s a list of the most common arithmetic operators:

OperatorNameExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Remaindera % b
++Increment (increments by 1)++a or a++
Decrement (decrements by 1)–a or a–
**Exponentiation (Power)a ** b

Example of Arithmetic Operators:

let x = 8;
let y = 4;

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

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

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

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

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

// increment
console.log('++x = ', ++x); // x is now 9
console.log('x++ = ', x++); // prints 9 and then increased to 10
console.log('x = ', x);     // 10

// decrement
console.log('--x = ', --x); // x is now 9
console.log('x-- = ', x--); // prints 9 and then decreased to 8
console.log('x = ', x);     // 8

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

Note: the ** operator was introduced in ECMAScript 2016 and may not be supported by all browsers.

Comparison Operators in JavaScript

Comparison operators compare two values and return either true or false as a boolean value. For example:

const x = 5, y = 4;
console.log(a > b); // true

The comparison operator > is used to see if x is greater than y in this case.

Here’s a list of the most common comparison operators:

OperatorDescriptionExample
==Equal to: returns true if the operands are equala == b
!=Not equal to: returns true if the operands are not equala != b
===Strict equal to: true if the operands are equal and of the same typea === b
!==Strict not equal to: true if the operands are equal but of different types or not equal at alla !== b
>Greater than: true if the left operand is greater than the right operanda > b
>=Greater than or equal to: true if the left operand is greater than or equal to the right operanda >= b
<Less than: true if the left operand is less than the right operanda < b
<=Less than or equal to: true if the left operand is less than or equal to the right operanda <= b

Example of Comparison Operators:

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

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

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

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

Logical Operators in JavaScript

Logical operators carry out logical operations and return a true or false boolean value. For example:

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

The logical operator AND(&&) is used here. The result is true because both x < 9 and y < 6 are true.

Here’s a list of the most common logical operators:

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

Example of Logical Operators:

// 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

Bitwise Operators in JavaScript

Operations on binary representations of numbers are performed by bitwise operators.

Here’s a list of the most common bitwise operators:

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

String Operators in JavaScript

You can also use the + operator in JavaScript to concatenate (join) two or more strings.

Example of String Operators:

// concatenation operator
console.log('Developers' + 'dome');   //  Developersdome

let a = 'JavaScript';

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

Note: The + concatenates(joins) strings when used with them. However, when + is used with numbers, it performs addition.

Other JavaScript Operators

The following is a list of other JavaScript operators:

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

That’s all about the Operators in JavaScript.

You may like Datatypes in JavaScript.

Hope this article will guide you to recognize the Operators in JavaScript that you needed and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has One Comment

Leave a Reply