# Quirky behaviour of "\<space\>"

**URL:** <https://es.discourse.group/t/quirky-behaviour-of-space/972>\
**Category:** I have questions\
**Created:** [September 6, 2021, 8:01am UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972 "2021-09-06T08:01:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [September 6, 2021, 8:01am UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/1 "2021-09-06T08:01:31Z")

</div>

I have lately been bugged with an issue of how js consider certain strings being true / false in a boolean context;

```javascript
let x = Boolean(" ") // Boolean("<space>")
console.log(x) // true

let y = " " == false // "<space>" == false
console.log(y) // true

```

Why is `"<space>"` considered **true** when passed to the Boolean constructor and at the same time considered equivalent to **false** using the `==` operator?

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [September 6, 2021, 9:17am UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/2 "2021-09-06T09:17:01Z")

</div>

Because [loose comparison](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islooselyequal) is utterly ridiculous :D  
In particular, you're hitting step 10.

> If [Type](https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-ecmascript-data-types-and-values)(y) is Boolean, return [IsLooselyEqual](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islooselyequal)(x, ! [ToNumber](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tonumber)(y)).

```javascript
> '-0E+123' == false
true
> '123E-400' == false
true

```

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [September 6, 2021, 10:04am UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/3 "2021-09-06T10:04:51Z")

</div>

why does this behaviour occur only for `"<space>"` and not for "%", "\_", or any other strings?? why does loose equality treat it as an empty string?

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [September 6, 2021, 10:32am UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/4 "2021-09-06T10:32:03Z")

</div>

If you follow the algorithm, both sides of `<string> == <boolean>` get eventually coerced to Number.

> 1. If [Type](https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-ecmascript-data-types-and-values)(y) is Boolean, return [IsLooselyEqual](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islooselyequal)(x, ! [ToNumber](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tonumber)(y)).
> 2. If [Type](https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-ecmascript-data-types-and-values)(x) is String and [Type](https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-ecmascript-data-types-and-values)(y) is Number, return [IsLooselyEqual](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islooselyequal)(! [ToNumber](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tonumber)(x), y).

So:

```javascript
"string" == false
// becomes
"string" == Number(false)
// becomes
Number("string") == Number(false)

```

```javascript
" " == false
// because
Number(false) === 0
Number(" ") === 0

```

```javascript
"%" != false
// because
Number("%") == NaN
NaN !== 0

```

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [September 6, 2021, 12:18pm UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/5 "2021-09-06T12:18:16Z")

</div>

Number(" ") === 0  
but how is this coercion done??  
Based on unicode code point of `<space>`??  
but that is U+0020.  
Null has the corresponding U+0000 code point.  
Number(" ") === 0; how is this coercion even reasonable??  
I mean if:

```javascript
Number("<some-glyph>") === NaN
// and <space> should also be
Number("<space>") === NaN
// it's illogical and inconsistent for it to be 0
Number("") === 0
// here it makes some sense for an empty
// string to be coerced to 0

```

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [September 6, 2021, 12:43pm UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/6 "2021-09-06T12:43:26Z")

</div>

> [@jithujoshyjy](#):
>
> Number(" ") === 0; how is this coercion even reasonable??

It is not. But that's how it was "standardized".

> [@jithujoshyjy](#):
>
> ```javascript
> Number("") === 0
> // here it makes some sense for an empty
> // string to be coerced to 0
> 
> ```

To me that doesn't make sense, either. There is no number in an empty string, it should've returned NaN.

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [September 6, 2021, 1:07pm UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/7 "2021-09-06T13:07:15Z")

</div>

Missed this question:

> [@jithujoshyjy](#):
>
> Number(" ") === 0  
> but how is this coercion done??  
> Based on unicode code point of `<space>`??

No. `Number("string")` tries to parse it, stripping leading/trailing whitespace. A string of whitespace is treated like an empty string.

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [September 6, 2021, 2:23pm UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/8 "2021-09-06T14:23:27Z")

</div>

barbaric logic of standardisation; sad but we can't do much about it😠  
Anyway thanks @lightmare for helping me out here!

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [September 6, 2021, 2:30pm UTC](https://es.discourse.group/t/quirky-behaviour-of-space/972/9 "2021-09-06T14:30:04Z")

</div>

So to wrap up here's what I understood:

```javascript
// the normalisation process:

// step.1
" " == false 

// step.2
stripSpaces(" ") == false

// step.3
Number("") == Number(false)

// step.4
0 == 0

// step.5
true

```
