Arithmetic in JavaScript
Posted by Superadmin on May 01 2023 06:24:37

Arithmetic in JavaScript

By Priya PedamkarPriya Pedamkar
  

Arithmetic in JavaScript

Overview of Arithmetic in JavaScript

Arithmetic operators in JavaScript take input in the form of numerical values (i.e. either variables or literals) as their arguments/operands and also return output as a numerical value. Basically, the standard arithmetic operators are namely, multiplication (*), division (/), addition (+) and subtraction (-).

All of the above operators operate as they behave in other programming languages such as Java, C++, etc. except the division (/) operator which always returns a floating-point value as output in JavaScript, in comparison to truncated values in the languages such as C, Java.

For Example:

In JavaScript 1/4 returns 0.25 and in Java ¼ is returned as 0 (in case of int type). In addition to the above, JavaScript also provides further arithmetic operators such as modules (%) operator, exponential operator (**), increment (++) & decrement(–) operator and unary operators like unary minus (-) & unary addition (+).

List of Arithmetic Operators in JavaScript

All the Arithmetic Operators used in JavaScript are explained below with briefly explaining their functionality with examples:

1. Addition Operator (+)

The Addition operation will do the sum of the two numeric arguments/operands or perform concatenation operation.

Syntax:

a + b

Example:

console.log(5 + 4);           // 9 Int + Int => Addition
console.log(5 + true);        // 6 Int + Boolean => Addition
console.log(true + false);    // 1 Boolean + Boolean => Addition
console.log(true + 'Self');   // trueSelf Boolean + String => Conactenation
console.log('Your' + 'Self'); // YourSelf String + String => Concatenation
console.log(4 + 'Self');      // 4Self Int + String => Concatenation

2. Subtraction Operator (-)

The Subtraction operation will find out the difference between the given two arguments/operands.

Syntax :

a - b

Example:

console.log(5 - 4); // 1
console.log(2 - 5); // -3
console.log(5 - true); // 4
console.log(5 - 'Your'); // NaN

3. Multiplication Operator (*)

The Multiplication Operation will result in the product of the given two arguments/operands.

Syntax:

a * b

Example:

console.log(5 * 4); // 20
console.log(-5 * 4); // -20
console.log(5 * 'Your'); // NaN
console.log(0 * Infinity); // NaN
console.log(Infinity * Infinity); // Infinity

4. Division Operator (/)

The Division Operator will give the value of quotient resulted from the arguments/operands where the left operand is the dividend and the right one is the divisor.

Syntax:

a / b

Example:

console.log(1 / 4); // 0.25
console.log(1 / 2); // 0.5
console.log(0.1 / 0.4); // 0.25
console.log(4.0 / 0); // Infinity
console.log(-4.0 / 0); // -Infinity

5. Modulus Operator (%)

The Modulus Operator will result in the remainder of the given two arguments/operand division result. It always takes the dividend sign.

Syntax:

a % b

Example:

console.log(15 % 4); // 3
console.log(1 % 4); // 1
console.log(-1 % 4); // -1
console.log(15 % -4); // 3
console.log(NaN % 4); // NaN
console.log(15.5 % 4); // 3.5

6. Exponential Operator (**)

The Exponential Operator will result in the value of the first operand raised to the power of the second operand. The Exponential Operator is right-associative in JavaScript i.e. a ** b ** c means a ** (b ** c).

Note- The precedence of the exponential operator in other languages such as Python and PHP, is greater than unary operators like unary plus, unary minus, etc. with few exceptions but in JavaScript, we cannot create ambiguous statements because any unary operator cannot be placed directly in front of a base value.

Syntax:

a ** b

Example:

console.log(-5 ** 5); // SyntaxError: Unexpected token **
console.log(2 ** 2); // 4
console.log(-(5 ** 5)); // -3125
console.log(0.5 ** 5); // -0.03125
console.log(5 ** 0.5); // 2.23606797749979
console.log(0.5 ** 0.5); // 0.7071067811865476
console.log(5 ** (-5)); // 0.00032

7. Increment Operator (++)

The Increment Operator is a Unary operator which will add 1 to its argument/operand. If it is used as a prefix (i.e the operator is used prior to the operand ++a) then the value gets incremented first and then the value is returned. If it is used as a postfix (i.e the operator is used after the operand a++) then the value gets returned first and then it is incremented.

Syntax:

++a or a++

Example:

var a = 5;
console.log(++a); // 6 prefix
var a = 4;
console.log(a++); // 4 postfix

8. Decrement Operator (–)

The Decrement Operator is a Unary operator that will decrease 1 from its argument/operand. If it is used as a prefix (i.e the operator is used prior to the operand –a) then the value gets decremented first and then the value is returned. If it is used as a postfix (i.e the operator is used after the operand a–) then the value gets returned first and then it is decremented.

Syntax:

--a or a--

Example:

var a = 5;
console.log(--a); // 4 prefix
var a = 4;
console.log(a--); // 4 postfix

9. Unary Minus (-)

The Unary Minus is a unary operator which will result in the negation of the operand followed by it.

Syntax:

-a

Example:

var a = 5;
b = -a;
console.log(a,b); // a = 5 b = -5
var a = '5'; // Unary Minus can convert non-numbers to number
b = -a;
console.log(a,b); // a = 5 b = -5

10. Unary plus (+)

The Unary Plus Operator is basically used to convert its operands into a number. Although unary minus also can convert non-numbers to numbers this operator does it very fast and is the most used method to convert the operands into numbers as it does not perform any other operation other than this. Unary Plus can be used to convert a string, representations of float and integers and also non-string values such as true, false, null, etc.

Syntax:

+a

Example:

console.log(+5); // 5
console.log(+'5'); // 5
console.log(+true); // 1
console.log(+null); // 0
console.log(+false); // 0
console.log(+function(value){return value}); // NaN

Conclusion

In JavaScript, we can use Arithmetic Operators to perform all the logical and arithmetic computing needed to perform all day to day needs of the business. In JavaScript arithmetic operators always return the exact value in terms of floating-point, hence it is better in computing skills than other programming languages. At last, in order to reduce ambiguity in statements, JavaScript prefers to use brackets before unary operators to perform the calculations correctly and without any errors.