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
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-12-almost-but-not-quite.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<code>
<pre lang="javascript">
alert(111111111111111111111); // alerts 111111111111111110000
</code>
</pre>
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-12-foonanny.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Evaluted as `"foo" + (+ "bar")`, which converts “bar” to not a number.

Proposal to rename this site: NaNwtf

<code>
<pre lang="javascript">
("foo" + + "bar") === "fooNaN" // true
</code>
</pre>

**Explanation**
1. `+"bar"` calls `"bar".valueOf()` which tries to return a number
Expand Down
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-12-function-context-fun.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<code>
<pre lang="javascript">
(x=[].reverse)() === window // true
</code>
</pre>

- Thanks to [@tobeytailor](http://twitter.com/tobeytailor) for pointing out this beauty.
- Updated: an error is thrown when the code is executed in Chrome 49
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-12-maths-fun.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Clearly JavaScript is not the most beautiful, or intuitive, language for maths.

```
<pre lang="javascript">
typeof NaN === 'number' // true
Infinity === 1/0 // true
0.1 + 0.2 === 0.3 // false
```
</pre>
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-12-min-number-treachery.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<code>
<pre lang="javascript">
Number.MIN_VALUE > 0;
// true? really? wtf.
</code>
</pre>

It turns out that MIN_VALUE is the smallest number GREATER THAN ZERO, which of course totally makes sense.
It turns out that MIN_VALUE is the smallest number GREATER THAN ZERO, which of course totally makes sense.
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-12-not-a-number-is-a-number.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<code>
<pre lang="javascript">
typeof NaN // number, of course.
</code>
</pre>

Now that makes sense.
Now that makes sense.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Some argue this makes sense. Some ppl also like to sniff glue.

<code>
<pre lang="javascript">
NaN === NaN // false
</code>
</pre>
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-12-null-is-not-an-object.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```
<pre lang="javascript">
typeof null // object
null instanceof Object // false
```
</pre>

Classic null is not an Object.
Classic null is not an Object.
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-12-parseint-treachery.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```
<pre lang="javascript">
parseInt('06'); // 6
parseInt('08'); // 0
```
</pre>

This is because parseInt accepts a second argument for radix. If it is not supplied and the string starts with a 0 it will be parsed as an octal number. Riiiiiiight, of COURSE.
This is because parseInt accepts a second argument for radix. If it is not supplied and the string starts with a 0 it will be parsed as an octal number. Riiiiiiight, of COURSE.
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-13-null-is-not-falsy.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```
<pre lang="javascript">
[] == false; // true
"" == false; // true
null == false; // false, that's more like it
```
</pre>
8 changes: 4 additions & 4 deletions src/shared/md/2010-02-13-string-is-not-string.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
```
"string" instanceof String; // false.
<pre lang="javascript">
"string" instanceof String; // false.
// 'course it isn't not a string, it may look like a string
// but actually it's masquerading as a banana.
```
</pre>

When is a string, not a string? When it’s a duck - [@rem](http://twitter.com/rem)
When is a string, not a string? When it’s a duck - [@rem](http://twitter.com/rem)
8 changes: 4 additions & 4 deletions src/shared/md/2010-02-15-accidental-global.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
This one is fun and sneaky.
This one is fun and sneaky.

```
<pre lang="javascript">
(function(){
var x = y = 1;
})();
alert(x); // undefined
alert(y); // 1 -- oops, auto-global!
```
</pre>

It’s treated like: var x = (y = 1); thus, “y=1” creates an auto-global since there’s no binding “var” statement for it. Afterwards, that value gets copied into the properly defined local var “x”.
It’s treated like: var x = (y = 1); thus, “y=1” creates an auto-global since there’s no binding “var” statement for it. Afterwards, that value gets copied into the properly defined local var “x”.
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-careful.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<code>
<pre lang="javascript">
[] == ![] // true
</code>
</pre>

Arrays evaluate as true in a boolean condition so this does make some sense even if it doesn’t read very well! Thanks to [@collintmiller](http://twitter.com/collintmiller)!
Arrays evaluate as true in a boolean condition so this does make some sense even if it doesn’t read very well! Thanks to [@collintmiller](http://twitter.com/collintmiller)!
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-coerce-equality.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<code>
<pre lang="javascript">
3 == "3" // true
</code>
</pre>

[@robertnyman](http://twitter.com/robertnyman) reminding us why === is a best practice.
[@robertnyman](http://twitter.com/robertnyman) reminding us why === is a best practice.
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-firebug-reserved.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```
<pre lang="javascript">
// whilst in firebug, try:
var console = {}; // or, well - anything actually...
```
</pre>

Firebug absolutely won’t let you create a variable called console, try and it’ll kick you in the happy sack - [@rem](http://twitter.com/rem)
Firebug absolutely won’t let you create a variable called console, try and it’ll kick you in the happy sack - [@rem](http://twitter.com/rem)
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-hoisting.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
```
<pre lang="javascript">
(function(){
alert(window); // "undefined"
var window = window;
})();
```
</pre>

Because of “hoisting”, all variable *declarations* will be executed immediately at the top of a function scope. However, the variable *initializations* are not hoisted. So, local variable “window” is declared but uninitialized/”undefined”. wt-fun!
Because of “hoisting”, all variable *declarations* will be executed immediately at the top of a function scope. However, the variable *initializations* are not hoisted. So, local variable “window” is declared but uninitialized/”undefined”. wt-fun!
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-more-coerce-fun.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<code>
<pre lang="javascript">
3 == "03" // true!
</code>
</pre>

[@dandean](http://twitter.com/dandean) tweets, Don’t mind that zero there. I’m _sure_ it’s not important.

Bwahahaha!
Bwahahaha!
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-scope-fun.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@kriskowal](http://twitter.com/kriskowal), of [CommonJS](http://commonjs.org) fame, points out lexical scoping could solve this issue. Ah, JavaScript you are a dirty little language. Dirty dirty…slut! Ya you like those global variables… aaaaaw yah…

```
Object.prototype.foo = 10;
<pre lang="javascript">
Object.prototype.foo = 10;
console.log(foo); // 10
```
</pre>
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-thc-timetravel.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

Woah!

```
<pre lang="javascript">
var bignum = 1e300;
var ∞ = 1e400; //Infinity
alert(∞ - bignum); //Infinity
```
</pre>

Also note: [@sh1mmer](http://twitter.com/sh1mmer) is from the future wherein Unicode entities are valid for variable identifiers. Rename above to something sane in our old school present day interpreters.
Also note: [@sh1mmer](http://twitter.com/sh1mmer) is from the future wherein Unicode entities are valid for variable identifiers. Rename above to something sane in our old school present day interpreters.
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-15-timeout-delay.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
For Firefox only, any function executed by a setTimeout or setInterval invocation will get passed to it (whether you want it to or not) a mysterious “lateness” variable, which represents the number of milliseconds late the function is in executing. Sucks because it can clobber an intentionally unpassed “default” variable to your function. Sucks even more if this variable is intended to be boolean, because you end up with ”random” true/false’y values.

```
<pre lang="javascript">
// for FF only
setTimeout(function(rand){ alert(rand); },10); // FF passes a "magic" param we call "rand"
for (var i=0; i<100000; i++) { i; } // take some time
```
</pre>
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-15-true-has-a-value.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[@AtomFusion](http://twitter.com/AtomFusion) shows us that true sometimes has a value.

```
<pre lang="javascript">
(true + 1) === 2;​ ​// true
(true + true) === 2; // true
true === 2; // false
true === 1; // false
```
</pre>

Wow wtf.
Wow wtf.
10 changes: 5 additions & 5 deletions src/shared/md/2010-02-15-undefined-is-mutable.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
In JavaScript, undefined is nothing but a global variable name without a default value. Therefore, its primitive value is undefined. You can change the value of undefined:

```
<pre lang="javascript">
var a = {};
a.b === undefined; // true because property b is not set
undefined = 42;
a.b === undefined; // false
```
</pre>

Due to the mutability of undefined, it is generally a better idea to check for undefined-ness through typeof:

```
<pre lang="javascript">
var a = {};
typeof a.b == 'undefined'; // always true
```
</pre>

– [@mathias](http://mathiasbynens.be/)
– [@mathias](http://mathiasbynens.be/)
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-16-automagic-semicolons.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
I’m certain that this will end all debate about where curly braces belong… right?

```
<pre lang="javascript">
function laugh()
{
return
Expand All @@ -10,6 +10,6 @@ I’m certain that this will end all debate about where curly braces belong… r
}
laugh();
// returns undefined
```
</pre>

See also: [Automatic semicolon insertion](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion)
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-16-more-floating-point-rounding.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
In my quest to find whether Infinity was a finite number, I found this. Two numbers that are different are the exact same. - [@rem](http://twitter.com/rem)

```
<pre lang="javascript">
// note the number to the left of the 'e', 7 and 8 respectively
alert( 1.7976931348623157e+308 === 1.7976931348623158e+308 ); // true!
```
</pre>
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-16-reserved-for-future-use-maybe.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
JavaScript has a [ludicrous list of reserved words](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Reserved_Words); most of them aren’t even used as keywords in the language. Modern browsers allow using most of these words as identifiers, despite what the spec says. But Safari doesn’t like enum for some reason.

```
<pre lang="javascript">
// In Safari, try...
var enum;
//-> SyntaxError: Parse error
```
</pre>
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-19-concat-coerce.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
More concat “fun”.

```
<pre lang="javascript">
"3" + 1 // '31'
"3" - 1 // 2
"222" - -"111" // 333 (⊙﹏⊙)
```
</pre>
14 changes: 7 additions & 7 deletions src/shared/md/2010-02-23-arguing-with-arguments.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Nice arguments hackery going on here.
Nice arguments hackery going on here.

```
<pre lang="javascript">
(function(a,b,c) {
print(a,b,c); // one two three
print(arguments[0], arguments[1], arguments[2]); // one two three
Expand All @@ -18,9 +18,9 @@ Nice arguments hackery going on here.
args[1] = "up your";
args[2] = "dataz";
}
```
```
</pre>

<pre lang="javascript">
// A more real world example:
(function(arg1, arg2) {
print(arg1, arg2);
Expand All @@ -29,6 +29,6 @@ Nice arguments hackery going on here.
print(arg1, arg2);
//-> dose undefined
})('uno', 'dos');
```
</pre>

Thanks to [@deadlyicon](http://twitter.com/deadlyicon)!
Thanks to [@deadlyicon](http://twitter.com/deadlyicon)!
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-23-boolean-paradox.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Boolean logic paradox via [@amattie](amattie)!

```
<pre lang="javascript">
"0" && {} // true
0 && {} // false, ok...fair enough
0 == "0" // true, wtf!
```
</pre>
4 changes: 2 additions & 2 deletions src/shared/md/2010-02-23-coerced.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Careful w/ number type coercion! Thanks [@kelemen_viktor](http://twitter.com/kelemen_viktor)!

<code>
<pre lang="javascript">
a = 012 // 10
</code>
</pre>

That's why JavaScript assumes (for whatever reason) that the number is octal (e. g. `parseInt('012', 8)`).
6 changes: 3 additions & 3 deletions src/shared/md/2010-02-23-declaration-vs-initialization.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
```
<pre lang="javascript">
var a = 8;
var someFunc = function(){
document.write(a);
var a = 8;
};
someFunc(); // writes undefined
```
</pre>

Of course, the variable is undefined because its being declared but not initialized until after the document.write in the function context which itself runs before the first declaration of a. Yowza. Cheers to [Boaz, Al & Rick](http://weblog.bocoup.com/weird-var-behavior-in-javascript) for this one!
Of course, the variable is undefined because its being declared but not initialized until after the document.write in the function context which itself runs before the first declaration of a. Yowza. Cheers to [Boaz, Al & Rick](http://weblog.bocoup.com/weird-var-behavior-in-javascript) for this one!
8 changes: 4 additions & 4 deletions src/shared/md/2010-02-23-makes-perfect-sense.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Ha! This one is great.
Ha! This one is great.

<code>
<pre lang="javascript">
3 > 2 > 1 // false
</code>
</pre>

Remember how [true sometimes has a value](https://wtfjs.com/wtfs/2010-02-15-true-has-a-value) so in the above 3 > 2 evaluates to true making the second part of the expression evaluate true > 1 which is false. Of course! Thanks to [void_0](http://twitter.com/void_0) for this one!
Remember how [true sometimes has a value](https://wtfjs.com/wtfs/2010-02-15-true-has-a-value) so in the above 3 > 2 evaluates to true making the second part of the expression evaluate true > 1 which is false. Of course! Thanks to [void_0](http://twitter.com/void_0) for this one!
Loading