admin管理员组

文章数量:1353308

In JavaScript, why is:

new String * 1
<- 0

What exactly makes it return 0, and why is the following equal to NaN?

function Foo() { this.bar = 0; }
new Foo * 5;
<- NaN

In JavaScript, why is:

new String * 1
<- 0

What exactly makes it return 0, and why is the following equal to NaN?

function Foo() { this.bar = 0; }
new Foo * 5;
<- NaN
Share Improve this question edited May 25, 2016 at 14:09 mariocatch asked May 25, 2016 at 13:47 mariocatchmariocatch 8,72312 gold badges51 silver badges76 bronze badges 9
  • 2 What did you expect it to be if not 0? Also "" * 1 or {valueOf() { return 0; }} * 1} have the same result. – Bergi Commented May 25, 2016 at 13:50
  • Or: Try Number(""), Number(new String) and Number(new Foo) – Bergi Commented May 25, 2016 at 13:51
  • 2 @Bergi NaN perhaps? I'm asking why. I understand that those return 0, but that doesn't help explain it. – mariocatch Commented May 25, 2016 at 13:51
  • 1 @Bergi I can see where you're ing from, however debating why it was specified like that seems a bit fruitless. For most of us the why ends when we see the ratified specification this is typically considered the reason why not the how, the latter being the implementation logic that many js developers don't delve into. – David Barker Commented May 25, 2016 at 14:19
  • 1 @Bergi haha, no I don't, which is why I didn't answer this question with a statement that was ment worthy at best ;-) however it does go, in this instance ,a long way to a reason for the behaviour enquired about. – David Barker Commented May 25, 2016 at 14:27
 |  Show 4 more ments

3 Answers 3

Reset to default 10

If you use arithmetic operators like *, JavaScript will try to convert the type to a number. An empty string bees 0.

If you have, for example:

new String("foo") * 1

You will notice it returns NaN because the conversion to a number could not be pleted. That's what happens in your second situation.

When you use arithmetic operators, the operands will be converted to numbers first.

In the first case, on your left hand side expression you have an empty string. Empty strings will bee 0, when converted to a number, as per this section in the specification.

A StringNumericLiteral that is empty or contains only white space is converted to +0.

Since new String is just a string object with zero characters, it is empty and that is why your first expression is evaluated to zero (0 * 1).


In the second case, new Foo returns an object that cannot be converted to a number. If an object cannot be converted to a number, then NaN will be returned. Quoting the same section on the spec,

If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

That is why the result is NaN here (NaN * 5).


You can check these things yourselves.

console.log(+(new String()));
// 0
console.log(+{});
// NaN

new String returns a String object, which has a primitive value of ""

"" parsed as a number is 0.

So when you do new String * 1, it is as if you did 0 * 1.

Your Foo object does not specify any kind of primitive value, so it cannot work that out for you and you get NaN

If you write new String in your console, you should see the primitive value in the result

本文标签: Why does new String * 1 return 0 in JavascriptStack Overflow