JavaScript Operators

Operators allow you to get two simple expressions and combine them to form a more complex expression.

We can classify operators based on the operands they work with. Some operators work with 1 operand. Most with 2 operands. Just one operator works with 3 operands.

In this first introduction to operators, we'll introduce the operators you are most likely familiar with: binary operators.

We already introduced one when talking about variables: the assignment operator =. You use =to assign a value to a variable:


let a = 3;

Let's now introduce another set of binary operators that you are already familiar with, from basic math.

Operator Description
+ Addition (also string concatention)
- Subtration (also unary minus)
* Multiplication
/ Division
% Remainder of division (or modulus)
++ pre or post increment
-- pre or post decrement

The addition operator (+)


let three = 1 + 2;
let four = three + 1;

Example


let three = 1 + 2;
let four = three + 1;
console.log(three);
console.log(four);

The + operator also serves as string concatenation if you use strings, so pay attention:


let three = 1 + 2;
three + 1; // 4
"three"+ 1;// three1

Example


let three = 1 + 2;
three + 1; // 4
console.log(three);
"three"+ 1;// three1
console.log(three);

The subtraction operator (-)


const two = 4 - 2;

The division operator (/)

Returns the quotient of the first operator and the second:


const result = 20 / 5; //result === 4
const newResult = 20 / 7; //newResult === 2.857142857142857

Example


const result = 20 / 5; //result === 4
const newResult = 20 / 7; //newResult === 2.857142857142857
console.log(result);
console.log(newResult);

If you divide by zero, JavaScript does not raise any error but returns the infinityvalue (or -infinity if the value is negative).


1 / 0- //Infinity
1 / 0//-Infinity

The remainder operator (%)

The remainder is a very useful calculation in many use cases:


const result = 20 % 5; //result === 0
const newResult = 20 % 7; //result === 6

Example


const result = 20 % 5; //result === 0
const newResult = 20 % 7; //result === 6
console.log(result);
console.log(newResult);

A remainder by zero is always NaNa special value that means "Not a Number":


1 % 0//NaN

The multiplication operator (*)

If you want to multiply two numbers to get the result use single *


1 * 2// 2

The exponentiation operator (**)

Raise the first operand to the power of the second operand


1 ** 2// 1
2 ** 1// 2
2 ** 2// 4
2 ** 8// 256
8 ** 2// 64

pre or post increment or decrement (++ or --)

++ and --are C style operators their position around the variable they are operating on is important. If the operator appears BEFORE the variable they will be evaluated immediately, if they appear AFTER the variable they will be evaluated only after the entire line has been resolved. For instance


let a = 5;
let b = x++;// y=5, x=6

let x = 5;
let y = ++x;// y=6, x=6

In the first example y is assigned the value of x(5) and then xincremented by one because the ++ operator appears AFTER x. In the second example x is incremented by one first because ++ appears BEFORE the xso yis given the value 6.

Example


let a = 5;
console.log(a);
let b = a++;// y=5, x=6
console.log(b);

let x = 5;
console.log(x);
let y = ++x;// y=6, x=6
console.log(y);

Additional shorthand operators exist when working on the same variable. For instance.


x = x + 5// is the same as.
x += 5