1

I am confused by the following code:

Boolean(' '); // true
Boolean(''); // false
+' '; // 0
+''; // 0

My assumptions:

  1. Since Boolean(' ') evaluates to true, ' ' is a truthy value.
  2. Since Boolean('') evaluates to false, '' is a falsy value.
  3. Since the unary + operator attempts to convert its operand into a number, it should convert a falsy value to 0, and a truthy value to 1.

Question: Why does the unary + operator convert a truthy value (i.e. ' ') to 0?

Please feel free to point out everything that is wrong about my assumptions, and anything that I am missing. Thank you so much!

=================

EDIT AFTER INITIAL THREE REPLIES

Thank you guys, you are great, and your are right! Here's what I found in the ES6 specs:

"The conversion of a String to a Number value (...:) This value is determined in two steps: first, a mathematical value (MV) is derived from the String numeric literal; second, this mathematical value is rounded as described below. The MV on any grammar symbol, not provided below, is the MV for that symbol defined in 11.8.3.1.

The MV of StringNumericLiteral ::: [empty] is 0.

The MV of StringNumericLiteral ::: StrWhiteSpace is 0.

The MV of StringNumericLiteral ::: StrWhiteSpaceopt StrNumericLiteral StrWhiteSpaceopt is the MV of StrNumericLiteral, no matter whether white space is present or not."

(Source: https://262.ecma-international.org/6.0/#sec-runtime-semantics-mv-s)

So that would explain why both +'' and +' ' evaluate to 0.

I was also confused why Boolean() did not evaluate both to false:

The abstract operation ToBoolean converts argument to a value of type Boolean according to Table 10: (...) String: Return false if argument is the empty String (its length is zero); otherwise return true.

Source: https://262.ecma-international.org/6.0/#sec-toboolean

So that's why Boolean(''); // false and Boolean(' '); // true.

Thank you so much for your help! :)

Julia
  • 21
  • 2
  • I would assume the string is having the whitespace trimmed, since `+' 1 '` returns `1` – evolutionxbox Jul 01 '22 at 12:26
  • 1
    This might be wrong since I'm no docs hero, but https://tc39.es/ecma262/multipage/abstract-operations.html#sec-runtime-semantics-stringnumericvalue is one of the internals used by the unary operator ( if you follow the trail through these docs) And the implementation of StringNumericValue specifies we return positive zero when we have a string of whitespace. – Shilly Jul 01 '22 at 12:34
  • @Shilly: Thanks for pointing me to the docs - I'm no docs hero either and this is the first time I looked at the ES6 specs. I did so research and added what I found to my question. Thank you! – Julia Jul 01 '22 at 13:14
  • I was pleasantly surprised that the duplicate answer also references the StringNumericValue specification, so all credit kinda goes to Felix for figuring this out in 2012. – Shilly Jul 01 '22 at 13:16
  • @evolutionxbox: Thank you, I didn't think of this. You're right. When converting a string to a number: "The MV [mathematical value] of StringNumericLiteral ::: StrWhiteSpaceopt StrNumericLiteral StrWhiteSpaceopt is the MV of StrNumericLiteral, no matter whether white space is present or not." URL: https://262.ecma-international.org/6.0/#sec-runtime-semantics-mv-s Thank you! – Julia Jul 01 '22 at 13:17

0 Answers0