Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions posts/2010-11-15-i-am-myself-but-also-not-myself.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ console.log(foo == foo);
Is it time to have a "maybe" operator? :-P

--- [@diogobaeder](http://twitter.com/diogobaeder)

The point is that when you do `foo == foo` it does evaluate to `true` because the `==` operator looks for equality and you are comparing `foo` to itself.

However, `foo == !foo` is executed in a different way. Let's split it in some steps:
1. `foo` remains `[0]`
2. `!foo` evaluates to `0`
3. `foo` then also evaluates to `0`
4. `0 == 0` evaluates to `true`

— [MarkLinus](https://github.com/MarkLinus)
11 changes: 11 additions & 0 deletions posts/2013-02-28-null,-undefined-and-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ Both should obviously fail, but return **true**. srsly, WTF JS?
— [@damienklinnert][1]

[1]:https://twitter.com/damienklinnert]

That's because the `.test` method stringifies its first argument in order to apply the provided regex over it. Hence,
by doing `/^[a-z]{1,10}$/.test(null);` you're actually providing `"null"` as argument. This same behaviour affects the examples below:

```
/^[a-z]{1,10}$/.test(false); // true, false -> "false"
/^[a-z]{1,10}$/.test(true); // true, true -> "true"
/^\[object Object\]/.test({}); // true, {} -> "[object Object]"
```

— [MarkLinus](https://github.com/MarkLinus)
11 changes: 11 additions & 0 deletions posts/2013-03-25-null-to-bool.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ Well, this is awkward.
— [@noway421][1]

[1]:https://twitter.com/noway421

In this case, `null` is not a boolean and that's why `null == false` returns `false`.

However, when you do `!null`, JavaScript tries to cast `null` to a boolean value before applying the `!` (not)
operator, and `null`, along with other "falsy" values as `""` (empty string), `0`, `undefined`, `NaN` and others,
evaluates to `false`.

For a more detailed explanation, you might want to read [this article](http://james.padolsey.com/javascript/truthy-falsey/),
by [James Padolsey](http://james.padolsey.com/).

— [MarkLinus](https://github.com/MarkLinus)
7 changes: 7 additions & 0 deletions posts/2013-06-20-negative-indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ Negative numbers mean different things to different functions on the Array proto
— [@markdalgleish][1]

[1]:https://twitter.com/markdalgleish

According to [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice#Parameters):
> If the index is negative, the splice method will begin that many elements from the end.

Which means that if `nums = [1, 2, 3, 4, 5]` and you do `nums.splice(-2)`, `nums` will lose its last two items and will become `[1, 2, 3]` (note that the second argument is not needed in this case). This also applies to `Array.prototype.slice` method.

— [MarkLinus](https://github.com/MarkLinus)