Friday, September 25, 2020
Home Coding & Programming List of All JavaScript Arithmetic Operators With Examples

List of All JavaScript Arithmetic Operators With Examples

JavaScript offers several operators to help us work with numbers. All these operators are binary operators, which means they operate on two operands.

The operands are the numbers in an arithmetic operation while an operator performs the operation between the two operands. JavaScript supports the following types of operators.

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Conditional (or ternary) Operators

In this post, my emphasis will be on the Arithmetic Operators. If you want to know more about the other operators which are supported by JavaScript, then click on any of them in the above list.

Performing arithmetic operations is a very common thing to do with any programming language. The JavaScript Arithmetic Operators include Addition, Subtraction, Multiplication, Division, Modulus, Exponentiation, Increment, Decrement, Unary. Each operator is described below along with its example.

JavaScript Arithmetic Operators

Addition (+)

The addition operator takes two numerical operands and gives their numerical sum. It also concatenates two strings or numbers.

Syntax:

a + b

 

Example:

// Number + Number => Addition

1 + 2 gives 3

 

// Number + String => Concatenation

5 + "hello" gives "5Hello"

 

Subtraction (-)

The subtraction operator gives the difference of two operands in the form of numerical value.

Syntax:

a - b

Example:

// Number - Number => Subtraction

10 - 7 gives 3

 

"Hello" - 1 gives Nan

Multiplication (*)

The multiplication operator gives the product of operands where one operand is multiplicand and another is multiplier.

Syntax:

a * b

 

Example:

// Number * Number => Multiplication

3 * 3 gives 9

-4 * 4 gives -16

 

Infinity * 0 gives NaN

Infinity * Infinity gives Infinity

'hi' * 2 gives NaN

Division (/)

The division operator provides the quotient of its operands where the right operand is the divisor and the left operand is the dividend.

Syntax: 

a / b

 

Example:

// Number / Number => Division

5 / 2 gives 2.5

1.0 / 2.0 gives 0.5

 

3.0 / 0 gives Infinity

4.0 / 0.0 gives Infinity, because 0.0 == 0

2.0 / -0.0 gives -Infinity

 

Modulus (%)

The modulus operator returns the remainder left over when a dividend is divided by a divisor. The modulus operator is also known as remainder operator. It takes the sign of the dividend.

Syntax:

a % b

 

Example:

// Number % Number => Modulus of the number

 

9 % 5 gives 4

-12 % 5 gives -2

1 % -2 gives 1

5.5 % 2 gives 1.5

-4 % 2 gives -0

 

NaN % 2 gives NaN

Exponentiation (**)

The exponentiation operator gives the result of raising the first operand to the power of the second operand. The exponentiation operator is right-associative. 

Syntax:

a ** b

 

In JavaScript, it is not possible to write an ambiguous exponentiation expression i.e. you cannot put an unary operator (+ / – / ~ / ! / delete / void) immediately before the base number.

Example:

// Number ** Number => Exponenial of the number

 

-4 ** 2 // This is an incorrect expression

-(4 ** 2) gives -16, this is a correct expression

2 ** 5 gives 32

3 ** 3 gives 27

3 ** 2.5 gives 15.588457268119896

10 ** -2 gives 0.01

2 ** 3 ** 2 gives 512

 

NaN ** 2 gives NaN

Increment (++)

The increment operator increments (adds one to) its operand and returns a value.

  • If used postfix with operator after operand (for example, x++), then it increments and returns the value before incrementing.
  • If used prefix with operator before operand (for example, ++x), then it increments and returns the value after incrementing.

Syntax: 

a++ or ++a

Example:

 
// Postfix

var a = 2;

b = x++; // b = 2, a = 3

 

// Prefix

var x = 5;

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

Decrement (–)

The decrement operator decrements (subtracts one from) its operand and returns a value.

  • If used postfix, with operator after operand (for example, x–), then it decrements and returns the value before decrementing.
  • If used prefix, with operator before operand (for example, –x), then it decrements and returns the value after decrementing.

Syntax:

a-- or --a

 

Example:

// Prefix

var x = 2;

y = --x; gives x = 1, y = 1

 

// Postfix

var x = 3;

y = x--; gives y = 3, x = 2

Unary (-)

This is a unary operator i.e. it operates on a single operand. It gives the negation of an operand.

Syntax:

-a

Example:

var a = 3;

b = -a; gives b = -3, a = 3

 

// Unary negation operator

// can convert non-numbers

// into a number

var a = "3";

b = -a; gives b = -3

Unary (+)

This is a way to convert a non-number into a number. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number.

Syntax:

+a

 

Example:

+4     gives 4

+'2'   gives 2

+true  gives 1

+false gives 0

+null  gives 0

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

List of All JavaScript Arithmetic Operators With Examples

JavaScript offers several operators to help us work with numbers. All these operators are binary operators, which means they operate on two operands. The operands...

How to Find a Website’s RSS Feed’s URL

Having access to the RSS feed of a website gives you the opportunity to stay up-to-date on everything that website publishes. Whether that’s a...

Laravel: String data, right truncated: 1406 Data too long for column (SOLVED)

I was working on a project today when i ran into the error message "String data, right truncated: 1406 Data too long for column...

How to Enable or Disable Chrome’s Autofill on Android Devices

Storing your login details and other information in Chrome and/or your Google Account isn’t as secure as using an encrypted password manager, but it’s...

Recent Comments