Home » Javascript Tutorial » Arithmetic Operations in JavaScript
JAvascript

Arithmetic Operations in JavaScript

Arithmetic Operations in JavaScript

Arithmetic operations in JavaScript work on numerical values and also return a numerical value. JavaScript arithmetic operators work the same way as basic elementary Mathematical operators. The operators are grouped into two groups, binary operators and unary operators. Binary operators work on two operants and return one results whilst unary operators work on one operant and returns one result.

Examples of binary operators we shall look into are:

  • addition
  • subtraction
  • multiplication
  • division
  • modulo (remainder)

Examples of unary operators we shall look into are:

  • increment
  • decrement

Addition (+)

The addition operator behaves in different ways depending on the data types of the operants.

  • If the operants are numeric, the addition operator returns the sum of the operants.
  • If the operants are strings, the strings are concatenated.
  • If the operants are boolean, they are implicitly converted to their numeric equivalents. false becomes 0 and true becomes 1.
  • For a numeric and a boolean, addition occurs.
  • If any one of the operants is a string, concatenation occurs.

For example:

// Number + Number => addition
console.log(23 + 3); // 26

// Boolean + Number => addition
console.log(true + 87); // 88

// Boolean + Boolean => addition
console.log(false + false); // 0
console.log(true + true); // 2

// Number + String => concatenation
console.log(23 + 'March'); // "23March"

// String + Boolean => concatenation
console.log('vic' + false); // "vicfalse"

// String + String => concatenation
console.log('Java' + 'Script') // "JavaScript"
// concatenation occurs even if its a number represented as a string
console.log(59 + "1"); // 591
console.log("1" + 59); // 159

Subtraction (-)

The subtraction operator behaves in different ways depending on the data types of the operants.

  • If the operants are numeric, the subtraction operator returns the difference of the operants.
  • If any one of the operants is a string, NaN is returned signifying an invalid operation.
// Number - Number => subtraction
console.log(23 - 3); // 20

// Boolean - Number => subtraction
console.log(true - 87); // -86

// Boolean - Boolean => subtraction
console.log(false - false); // 0
console.log(true - true); // 0

// Number - String => unsupported
console.log(23 - 'March'); // NaN

// String - Boolean => unsupported
console.log('vic' - false); // NaN

// String - String => unsupported
console.log('Java' - 'Script') // NaN
// here, a number represented as a string is implicitly converted to a number
console.log(59 - "1"); // 58
console.log("1" - 59); // -58

Division (/)

The division operator divides the operants and returns a numerical value except when one of the operators is a string type.

console.log(23 / 3); // 7.666666666666667
console.log(true / 87); // 0.011494252873563218

console.log(false / false); // NaN
console.log(true / true); // 1

console.log(23 / 'March'); // NaN

console.log('vic' / false); // NaN

console.log('Java' / 'Script') // NaN
// here, a number represented as a string is implicitly converted to a number
console.log(59 / "1"); // 59
console.log("1" / 59); // 0.01694915254237288

Multiplication (*)

The multiplication operator multiplies the operants and returns a numerical value except when one of the operators is a string type.

console.log(23 * 3); // 69
console.log(true * 87); // 87

console.log(false * false); // 0
console.log(true * true); // 1

console.log(23 * 'March'); // NaN

console.log('vic' * false); // NaN

console.log('Java' * 'Script') // NaN
// here, a number represented as a string is implicitly converted to a number
console.log(59 * "1"); // 59
console.log("1" * 59); // 59

Remainder (%)

The remainder operator divides the operants and returns a remainder except when one of the operators is a string type.

console.log(23 % 3); // 2
console.log(true % 87); // 1

console.log(false % false); // NaN
console.log(true % true); // 0

console.log(23 % 'March'); // NaN

console.log('vic' % false); // NaN

console.log('Java' % 'Script') // NaN
// here, a number represented as a string is implicitly converted to a number
console.log(59 % "1"); // 0
console.log("1" % 59); // 1

Increment (++)

The increment operator increments its operand by 1 and returns the value. It has 2 modes of operation, either postfix or prefix.

  • Postfix, for example x++, returns the value of x and then increments x.
  • Prefix, for example ++x, increments the value of x and returns it.

The examples below will help you understand it.

// Postfix
let x = 23;
y = x++; // x is assigned to y and then x is incremented

console.log(x); // 24
console.log(y); // 23

// Prefix
let m = 23; 
n = ++m; // m is first incremented and then assigned to n

console.log(m); // 24
console.log(n); // 24
// here, a boolean is implicitly converted to a number
// Postfix
let x = true;
y = x++;

console.log(x); // 2
console.log(y); // 1

// Prefix
let m = true; 
n = ++m; 

console.log(m); // 2
console.log(n); // 2

Decrement (–)

The decrement operator decrements its operand by 1 and returns the value. It has 2 modes of operation, either postfix or prefix.

  • Postfix, for example x–, returns the value of x and then decrements x.
  • Prefix, for example –x, decrements the value of x and returns it.

The examples below will help you understand it.

// Postfix
let x = 23;
y = x--;

console.log(x); // 22
console.log(y); // 23

// Prefix
let m = 23; 
n = --m; 

console.log(m); // 22
console.log(n); // 22
// here, a boolean is implicitly converted to a number
// Postfix
let x = true;
y = x--;

console.log(x); // 0
console.log(y); // 1

// Prefix
let m = true; 
n = --m; 

console.log(m); // 0
console.log(n); // 0