Skip to content
Related Articles

Related Articles

JavaScript Arithmetic Operators

Improve Article
Save Article
  • Last Updated : 05 Dec, 2022
Improve Article
Save Article

JavaScript Arithmetic Operators are the operators that operate upon the numerical values and return a numerical value. There are many operators in JavaScript. Each operator is described below along with its example.

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

Syntax:

a + b

Example:

Javascript




<script type="text/javascript" charset="utf-8">
    // Number + Number => Addition 
    let x = 1 + 2
    console.log(x)
      
    // Number + String => Concatenation
    let y =  5 + "hello" 
    console.log(y)
</script>


Output:

3
5hello

Subtraction (-) The subtraction operator gives the difference of two operands in the form of numerical value.

Syntax:

a - b

Example:

Javascript




<script type="text/javascript" charset="utf-8">
    // Number - Number => Subtraction 
     let x = 10 - 7 
     console.log(x)
      
    let y = "Hello" - 1 
    console.log(y)
</script>


Output:

3
NaN

Multiplication (*) The multiplication operator gives the product of operands where one operand is a multiplicand and another is multiplier.

Syntax:

a * b

Example:

Javascript




<script type="text/javascript" charset="utf-8">
    // Number * Number => Multiplication
    let x = 3 * 3 
    let y = -4 * 4 
    console.log(x)
    console.log(y)
      
      
    let a = Infinity * 0 
    let b = Infinity * Infinity 
    console.log(a)
    console.log(b)
    let z = 'hi' * 2 
      console.log(z)
</script>


Output:

9
-16
NaN
Infinity
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:

Javascript




<script type="text/javascript" charset="utf-8">
    // Number / Number => Division
    let x = 5 / 2
    let y = 1.0 / 2.0
    console.log(x)
    console.log(y)
      
      
    let a = 3.0 / 0
    let b = 4.0 / 0.0
    console.log(a)
    console.log(b)
    let z = 2.0 / -0.0
    console.log(z)
</script>


Output:

2.5
0.5
Infinity
Infinity
-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 the remainder operator. It takes the sign of the dividend.

Syntax:

a % b

Example:

Javascript




<script type="text/javascript" charset="utf-8">
    // Number % Number => Modulus of the number
      
    let x = 9 % 5
    let y = -12 % 5
    let z = 1 % -2
    let a = 5.5 % 2
    let b = -4 % 2
    let c = NaN % 2
      
    console.log(x)
    console.log(y)
    console.log(z)
    console.log(a)
    console.log(b)
    console.log(c)
</script>


Output:

4
-2
1
1.5
0
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:

Javascript




<script type="text/javascript" charset="utf-8">
    // Number ** Number => Exponential of the number
      
    // let x = -4 ** 2 // This is an incorrect expression
    let y = -(4 ** 2) 
    let z = 2 ** 5 
    let a = 3 ** 3
    let b = 3 ** 2.5 
    let c = 10 ** -2 
    let d = 2 ** 3 ** 2 
    let e = NaN ** 2
      
    console.log(y)
    console.log(z)
    console.log(a)
    console.log(b)
    console.log(c)
    console.log(d)
    console.log(e)
</script>


Output:

-16
32
27
15.588457268119896
0.01
512
NaN

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

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

Syntax: 

a++ or ++a

Example:

Javascript




<script type="text/javascript" charset="utf-8">
    // Postfix 
    var a = 2;
    b = a++; // b = 2, a = 3
      
    // Prefix
    var x = 5;
    y = ++x; // x = 6, y = 6
      
    console.log(a)
    console.log(b)
    console.log(x)
    console.log(y)
</script>


Output:

3
2
6
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 the operator before the operand (for example, –x), then it decrements and returns the value after decrementing.

Syntax:

a-- or --a

Example:

Javascript




<script type="text/javascript" charset="utf-8">
    // Prefix
    var a = 2;
    b = --a; 
      
    // Postfix 
    var x = 3;
    y = x--;
       
     console.log(a)
     console.log(b)
     console.log(x)
     console.log(y)
</script>


Output:

1
1
2
3

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

Syntax:

-a

Example:

Javascript




<script type="text/javascript" charset="utf-8">
    var a = 3;
    b = -a; 
      
    // Unary negation operator
    // can convert non-numbers
    // into a number
    var x = "3";
    y = -x; 
        
      console.log(a)
      console.log(b)
      console.log(x)
      console.log(y)
</script>


Output:

3
-3
3
-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:

Javascript




<script type="text/javascript" charset="utf-8">
    let a =  +4     
    let b = +'2'   
    let c = +true  
    let x = +false 
    let y = +null
           
    console.log(a)
    console.log(b)
    console.log(c)
    console.log(x)
    console.log(y)
</script>


Output:

4
2
1
0
0

We have a complete list of Javascript operators, to check those please go through this Javascript Operators Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!