diff --git a/posts/2010-11-15-i-am-myself-but-also-not-myself.md b/posts/2010-11-15-i-am-myself-but-also-not-myself.md index 89a53bbb..61246a5d 100644 --- a/posts/2010-11-15-i-am-myself-but-also-not-myself.md +++ b/posts/2010-11-15-i-am-myself-but-also-not-myself.md @@ -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) diff --git a/posts/2013-02-28-null,-undefined-and-test.md b/posts/2013-02-28-null,-undefined-and-test.md index ef8a6365..da792a1b 100644 --- a/posts/2013-02-28-null,-undefined-and-test.md +++ b/posts/2013-02-28-null,-undefined-and-test.md @@ -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) diff --git a/posts/2013-03-25-null-to-bool.md b/posts/2013-03-25-null-to-bool.md index 8ccbee71..5bf703b1 100644 --- a/posts/2013-03-25-null-to-bool.md +++ b/posts/2013-03-25-null-to-bool.md @@ -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) diff --git a/posts/2013-06-20-negative-indexes.md b/posts/2013-06-20-negative-indexes.md index cd0a35fe..d9189421 100644 --- a/posts/2013-06-20-negative-indexes.md +++ b/posts/2013-06-20-negative-indexes.md @@ -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)