4

var fir=prompt("Enter first Number");
var sec=prompt("Enter second number");
var sum=Number(fir)+Number(sec);
alert("The sum is " + sum);
alert("The difference is " + fir-sec);
alert("The product is " + fir*sec);
alert("The division is " + fir/sec);

Now: suppose fir=2 and sec=1.

The output is:

3
NaN
2
2

Why is the difference NaN instead of 1?

Boann
  • 48,794
  • 16
  • 117
  • 146
AxidAcid
  • 49
  • 3
  • 2
    Add brackets + `alert("The Difference is " + (fir-sec));` – angel.bonev Oct 03 '19 at 10:58
  • Possible duplicate of [javascript addition program displaying "NaN" error](https://stackoverflow.com/questions/47734987/javascript-addition-program-displaying-nan-error) – Mike Doe Oct 03 '19 at 10:59
  • Because [type coercion](https://stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript) – Liam Oct 03 '19 at 11:00

5 Answers5

5

* and / have higher operator precedence than + and -, just like with PEMDAS order of operations in standard math. Specifically, + and - have precedence 13, whereas * and / have precedence 14.

When an expression has + and -s only, they're evaluated in left-to-right order. So, your code is equivalent to:

alert(("The Difference is " + fir) - sec);
alert("The product is " + (fir*sec));
alert("The division is " + (fir/sec));

In the second and third alert, fir and sec get combined into a single numeric expression before being concatenated with the prior string.

In the first alert, "The Difference is " + fir get put together first, resulting in another string (a string + a number results in another string). So then you have

alert((someString) - sec);

But someString - sec doesn't make sense - you can't subtract something from a string, so the expression resolves to NaN.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
2

You can do one thing

var fir=prompt("ENter 1st Number");
var sec=prompt("Enter second number");
var sum=Number(fir)+Number(sec);
var sub=Number(fir)-Number(sec);
alert("The Sum is " + sum);
alert("The Difference is " + sub);
alert("The product is " + fir*sec);
alert("The division is " + fir/sec);

Usually, the plus operator + sums numbers.

But, if the binary + is applied to strings, it merges (concatenates) them:

let s = "my" + "string"; alert(s); // mystring Note that if one of the operands is a string, the other one is converted to a string too.

For example:

alert( '1' + 2 ); // "12"
alert( 2 + '1' ); // "21"

See, it doesn’t matter whether the first operand is a string or the second one. The rule is simple: if either operand is a string, the other one is converted into a string as well.

However, note that operations run from left to right. If there are two numbers followed by a string, the numbers will be added before being converted to a string:

alert(2 + 2 + '1' ); // "41" and not "221"

String concatenation and conversion is a special feature of the binary plus +. Other arithmetic operators work only with numbers and always convert their operands to numbers.

For instance, subtraction and division:

alert( 2 - '1' ); // 1
alert( '6' / '2' ); // 3

reference https://javascript.info/operators

Shekhar
  • 51
  • 2
1

Because you're concatenating a string ("The difference is") with a number, obtaining a string, and then subtracting a number from it. Use parentheses

alert("The Difference is " + (fir-sec));
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
0

The problem you face is due to dynamic type conversion. In your second alert JS engine 1st concatenates "The Difference is " and fir and then subtracts sec. You have to add braces:

alert("The Difference is " + (fir-sec))

In all other cases it is ok because * and / operators have higher priority than + operator

Slim
  • 1,924
  • 1
  • 11
  • 20
0

Remeber Math's BODMAS theory,

in case of multiplication and division it works and giving you result.

But for substraction, it first does addition.

And finally:

String - Number = NaN

Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20