Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Arithmetic Operators in JavaScript

The JavaScript arithmetic operators take numerical values as their left & right operands, perform the arithmetic operation, and return a numerical value. The JavaScript supports all the arithmetic operators like addition (+), subtraction (-), multiplication (*), division (/), etc. Note that all the numbers in JavaScript are represented as IEEE 754 floating-point numbers and use floating-point arithmetic.

Arithmetic Operators

The following are the List of Arithmetic Operators in JavaScript

Operator Description
+ Addition
Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Remainder)
++ Increment
Decrement
+ Unary plus
Unary minus

Addition (+)

The addition operator (+) is a binary operator, which calculates the sum of two numeric operands.

                            
let a = 12
let b = 2
let c = a+b;
console.log(c)   //14
                     
                            
                        

If one of the operands is a string, then the + operator does a string concatenation

                            
let a = "Hello"
let b = 2
let c = a+b;
console.log(c)   //Hello2
                     
                            
                        
                            
    let a = "12"
     
     let b = 2
     let c = a+b;     //string concatenation as the b is string
     console.log(c)   //122
                     
                            
                        

The booleans are implemented as numerical values with a single binary digit (i.e., 0 & 1). 1 is true & 0 is false.

                            
let a = true     //boolean 
let b = 2
console.log(c)   //3 because true is 1
                     
                            
                        
                            
 
let a = false     //boolean 
let b = 2
let c = b+a;      
console.log(c)    //2 because false is 0
 
                     
                            
                        
                            
console.log(true+true+true)   //3  because true is 1.  
 
                     
                            
                        

Subtraction (–)

The subtraction operator subtracts the right operand from the left operand. If any of the operands is not a number, then it returns a NaN

                            
let a = 10
let b = 2
let c = a-b;
console.log(c)   //8
 
let d = b-a;
console.log(d)   //-8
 
 
//Boolean
console.log(true-false)  //1  true is 1, false is 0
console.log(2-true)      //1  
 
                            
                        
                            
                        

Converts strings to numbers.

                            
let a = "1"
let b = "2"
console.log(a-b);  //-1 
 
                            
                        
                            
                        

Subtraction, when one (or both) of the operand is not a number, always results in NaN

                            
let a = "Hello"
let b = 2
console.log(a-b);  //NaN
 
                            
                        
                            
                        

Multiplication (*)

The multiplication operator (*) multiplies the left operand with the right operand.

                            
let a = 5
let b = 2
console.log(a*b);  //10
 
                            
                        
                            
                        

Strings are converted to numbers.

                            
let a = "5"
let b = "2"
console.log(a*b);  //10
 
 
                            
                        
                            
                        

Infinity

                            
console.log(Infinity * 0)         // NaN
console.log(Infinity * Infinity)  // Infinity
 
 
                            
                        
                            
                        

Multiplication with non-numbers results in NaN.

                            
console.log("Hello" * 10)         // NaN   
 
                            
                        
                            
                        

Division (/)

The division operator (/) divides the left operand (dividend) with the right operand (divisor).

Example

                            
console.log(10 / 2);    //5
 
console.log(11 / 2);    //5.5  
 
                            
                        
                            
                        

Strings are converted to numbers.

                            
console.log(6 / '3');     //2
console.log('6' / '3');   //2
                            
                        
                            
                        

If the string is not a number, then the result is NaN.

                            
console.log(6 / 'a');   //NaN
console.log(6 / '3a');  //NaN
                        
                            
                        

Booleans are numbers. True is 1 & false is 0

                            
console.log(6 / true);   //6 true is 1
console.log(2 / false);  //Infinity false is 0
                        
                            
                        

Dividing by 0 results in Infinity

                            
console.log(2 / 0);      //Infinity
                        
                            
                        

Modulus or Reminder (%)

The remainder operator (%) returns the remainder leftover of a division operation between the operands. The result always takes the sign of the dividend.

                            
console.log(12%5)     //2
console.log(-12%5)    //-2
 
 
console.log(-12%-5)   //-2
console.log(12%-5)    //
                        
                            
                        

Unary plus (+) & Unary minus (–)

The exponentiation operator (**) returns the result of raising the first operand to the power of the second operand.

Unary plus & Unary minus operators in JavaScript

Exponentiation (**)

The unary plus operator (+) precedes its operand and converts it into a number. If it fails to convert the operand into a number, then it returns NaN. The unary (-) operator converts the operand into a number and negates it.

                            
console.log(3 ** 4);         // 81
 
console.log(10 ** -2);       // 0.01
 
console.log(2 ** 3 ** 2);    // 512
 
console.log((2 ** 3) ** 2);  //  64
 
console.log(2 ** (3 ** 2));  // 512
                        
                            
                        

BigInt & Arithmetic Operators

The BigInt can be used with the following arithmetic operations. Addition (+), Subtraction (-), Multiplication (*), Exponentiation (%), Division (/), Modulus (Remainder) (%), Increment (++), Decrement (–).

Unary plus (+) & Unary minus (-) are not supported

The / division operator rounds of the final result to the whole number. For example, dividing 5/2 results in 2 and not 2.5. i.e it is an integer and not decimal.