Operators in JavaScript with Examples

← PrevNext →

Operators in JavaScript are used to perform various mathematical and logical comparisons on data values or operands, to produce a result.

For example,

5 + 7 = 12

The + (plus sign) here, is an operator (an arithmetic operator), the number 5 is the left operand and the value 7 is the right operand.

In JavaScript, you can assign the data values (or the operands) to "variables" and apply an "operator" to get a result, or you can simply use numbers along with an operator and get the result. For example,

<script>
    var a = 5;
    var b = 7;
    var c = a + b;
    alert (c);

// Using ES6 features.
    // let a = 5;		
    // let b = 7;
    // let c = a + b;
    // alert (c); 	    // the answer is 12.
</script>
Try it

Or, simply use numbers with the + operator.

var result = 5 + 7;
alert (result);	    

// Using ES6 features.
// let result = 5 + 7;
// alert (result);	    // its 12 again.

Note: The + operator can also be used to concatenate (or add) strings. I have explained about it here.

Its basic math and we have done this before. Therefore, lets begin with the Arithmetic Operators.

JavaScript Arithmetic Operators

Table created using HTML Table Generator
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (gives the remainder obtained by dividing two operands or numbers)
More examples using modulus operator
++ Increment (It will increase value by 1)
-- Decrement (it will decrease the value by 1)

Try it

Using + Operator to Concatenate Values

The + (plus) operator is often used to add two or more numbers. However, it can also be used to concatenate two values of different data types. For example,

var name = 'alpha';
var age = 27;
alert (name + ' is ' + age + ' years old');

// Using ES6
let name = 'alpha';
let age = 27;
alert (name + ' is ' + age + ' years old');

We can use the + on different "data types" to get different results. For example,

// Concatenating two string values.	
let a = 'arun ', b = 'banik';
alert (a + b);	 // the result is arun banik	

// or 

// Concatenating a string with number.
let x = 3, y = ' dimension';
alert (x + y); 	    // the result is 3 dimension.
Try it

Interesting isn't?

Note: Be careful while using the + operator. The results can vary.
Can you spot the difference between the two examples below?

1)
let a, b;
a = 5;
b = 7;
document.getElementById('result').innerHTML = 'a + b ' + ' = ' + (a + b);	

2)
let a, b;
a = 5;
b = 7;
document.getElementById('result').innerHTML = 'a + b ' + ' = ' + a + b;

Ok, let’s move on to the next set of operators.

JavaScript Comparison Operators

Comparison operators in JavaScript are used to compare two given values, and the result is returned in a Boolean value, that is, true or false.

Table created using HTML Table Generator
Operator Description
== equal to Try it
=== compares equal value with equal type Try it
!= not equal (compares two operands that are not equal) Try it
> greater than Try it
< less than Try it
>= greater than or equal to Try it
<= less than or equal to Try it

JavaScript Conditional Operator

JavaScript also has a conditional operator called the ternary operator. It assigns a value to a varible after satisfying a conditon. Its actually works like the if...else statement. I have explained about ternary operator in detail here.

Syntax

Condition ? first_expression : second_expression


Try it

JavaScript Logical Operators

The logical operators are used to compare two or more conditions.

Operator Description
&& the logical AND operator Try it
|| the logical OR operator Try it
! the logical NOT operator Try it
(??) Nullish coalescing operator Try it

🚀 Learn more about Nullish coalescing operator (??)

JavaScript Assignment Operators

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

let a = 5;

Here, I have assigned the value 5 to the variable a using the = (or, equal assignment) operator. It can also be a = b, assigning value to a variable from another variable. There are many more assignment operators like this.

Operator Description
= Assign right operand value to the left operand. For example,
a = 5;
or
a = b;
Don’t get confused with "=" to "==" (see Comparison Operators)
Try it
+= Sum of two right operand values is assigned to the left operand. For example,
a = a + b;
or
a += b;
Try it
-= Subtract right operand value from the left operand and assign the result to the left operand. For example,
a -= 2;
or
a = a – 2;
Try it
*= Multiply right and left operand values and assign the result to the left operand. For example,
a *= 5;
or
a = a * 5;
or
a = a * b;
Try it
/= Divide left operand value with the right operand and assign the result to the left operand. For example,
a /= 5;
or
a = a / 5;
or
a = a / b;
Try it
%= Get the modulus of left operand, divide it by right operand and assign the result to the left operand. For example,
a %= 2;
or
a %= b;
or
a = a % b;
See more examples
Try it

← PreviousNext →