-7

I know in javascript, Array does not support + operation, so +[] would be convert to + [].toString(), but I can't figure out why +"" is equal to zero.

MarkoCen
  • 2,189
  • 13
  • 24
  • I think this is a unary operator. – evolutionxbox May 02 '16 at 15:29
  • It converts to zero because there is a specific rule in the ECMAScript spec that requires an empty string to be converted that way when performing numeric conversion. –  May 02 '16 at 15:32
  • The sequence in the ECMA spec is painful to follow and copy here (see https://tc39.github.io/ecma262/2016/#sec-tonumber especially), can't we find one of the many identical questions ? – Denys Séguret May 02 '16 at 15:33
  • `+[]` result is `0` and does not equal `[].toString()` – evolutionxbox May 02 '16 at 15:34
  • 1
    @evolutionxbox `+[]` is equal to `+ [].toString()` – MarkoCen May 02 '16 at 15:35
  • @MarkoCen I just tried it in my console... Why would it be anything else? The plus unary operator tries to cast to a number. An empty array will be 0. – evolutionxbox May 02 '16 at 15:36
  • @evolutionxbox: the algorithm used to convert an array to a number performs a `toString()` operation. Do this experiment: `x = []; x.toString = function() { return "123" }; console.log(+x);` –  May 02 '16 at 15:39
  • 1
    @squint ah. That makes sense – evolutionxbox May 02 '16 at 15:40
  • 1
    A related question: [All falsey values in JavaScript](http://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript). The harsh response to this question is surprising ... especially given the enlightening comments that it has produced. – Yogi May 02 '16 at 15:45
  • Here's a more direct [duplicate](http://stackoverflow.com/questions/3306453/why-an-empty-array-type-converts-to-zero). I don't think this was that bad of a question, but it has been asked several times in many different forms, so that may explain the votes. –  May 02 '16 at 15:50

1 Answers1

-1

Because empty string ("") is false in javascript (As Javascript is a dialect of ECMAScript, and ECMAScript language specification clearly defines this behavior) and + unary operator converts false to 0.

Reference: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

void
  • 36,090
  • 8
  • 62
  • 107
  • @evolutionxbox check now.. – void May 02 '16 at 15:31
  • 2
    It isn't because an empty string is false *(I assume you mean "falsey")*. There are other falsey values that are not converted to zero. –  May 02 '16 at 15:31
  • To be pedantic, `""` is falsey (not strictly false), so a coercion to boolean comes first, then boolean to number. – ssube May 02 '16 at 15:31
  • @squint All false, 0, empty strings '' and "", NaN, undefined, and null are always evaluated as false; everything else is true. – void May 02 '16 at 15:32
  • 1
    @void: They're "falsey", not `false`, which is a distinct value, but that doesn't have anything to do with how it's converted to a number. If they were always evaluated as `false`, then `"" == null` would be `true`. –  May 02 '16 at 15:33
  • This is just incorrect. This has _nothing_ to do with booleans or falsyness. The actual algorithm is [`ToNumber` with a string argument](https://tc39.es/ecma262/#sec-tonumber-applied-to-the-string-type): _“A_ StringNumericLiteral _that is empty or contains only white space is converted to `+0`.”_. – Sebastian Simon Feb 17 '21 at 07:09