Reputation: 49
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?
Upvotes: 4
Views: 83
Reputation: 370809
*
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
.
Upvotes: 5
Reputation: 51
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
Upvotes: 2
Reputation: 3629
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
Upvotes: 0
Reputation: 1964
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
Upvotes: 0
Reputation: 27119
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));
Upvotes: 1