Javascript Arithmetic Operator Add Operator

Introduction

The add operator (+) is used as shown in the following example:

let result = 1 + 2; 

If the two operands are numbers, they perform an arithmetic add and return the result according to the following rules:

  • If either operand is NaN, the result is NaN.
  • If Infinity is added to Infinity, the result is Infinity.
  • If -Infinity is added to -Infinity, the result is -Infinity.
  • If Infinity is added to -Infinity, the result is NaN.
  • If +0 is added to +0, the result is +0.
  • If -0 is added to +0, the result is +0.
  • If -0 is added to -0, the result is -0.

If, however, one of the operands is a string, then the following rules apply:

  • If both operands are strings, the second string is concatenated to the first.
  • If only one operand is a string, the other operand is converted to a string and the result is the concatenation of the two strings.
  • If either operand is an object, number, or Boolean, its toString() method is called to get a string value and then the previous rules regarding strings are applied. For undefined and null, the String() function is called to retrieve the values "undefined" and "null", respectively.

Consider the following:

let result1 = 5 + 5;    // two numbers 
console.log(result1);         // 10 
let result2 = 5 + "5";  // a number and a string 
console.log(result2);         // "55" 

This code illustrates the difference between the two modes for the add operator.

Normally, 5 + 5 equals 10.

However, if one of the operands is changed to a string, "5", the result becomes "55" because the first operand gets converted to "5" as well.

One of the most common mistakes in Javascript is being unaware of the data types involved with an addition operation. Consider the following:

let num1 = 5; 
let num2 = 10; 
let message = "The sum of 5 and 10 is " + num1 + num2; 
console.log(message);  // "The sum of 5 and 10 is 510" 

In this example, the message variable is filled with a string that is the result of two addition operations.

The first combines a string with a number (5), which results in a string.

The second takes that result (a string) and adds a number (10), which also results in a string.

To perform the arithmetic calculation and then append that to the string, add some parentheses like this:

let num1 = 5; /*ww  w. j  av a  2  s .c o m*/
let num2 = 10; 
let message = "The sum of 5 and 10 is " + (num1 + num2); 
console.log(message);  // "The sum of 5 and 10 is 15" 

Here, the two number variables are surrounded by parentheses, which instruct the interpreter to calculate its result before adding it to the string.

The resulting string is "The sum of 5 and 10 is 15".




PreviousNext

Related