Skip to content

Instantly share code, notes, and snippets.

@mr21
Last active November 28, 2015 12:03
Show Gist options
  • Save mr21/d5df92f541e992ec3aae to your computer and use it in GitHub Desktop.
Save mr21/d5df92f541e992ec3aae to your computer and use it in GitHub Desktop.
JavaScript - first lesson
<script>
var v = "0";
if (v && v == 0) { /* So it's `true` but... also `0` ? */
v = null;
if (v == undefined) { /* `null` but equal to `undefined` well... */
v = new Array(10, 11);
if (v[0] === 10 && v[1] === 11) { /* Oh! I understood this one! */
v = new Array(12);
if (v[0] == null) { /* Maybe a special case for `12` ...? */
if (7+1 === 8 && 7+1 == "8"
&& 7+1 === 010 && 7+1 != "010") { /* I know this, it's for octal number... */
v = ["0", "1", "101", "101"].map(parseInt);
if (v[0] === 0 && v[2] === v[3] / 2) { /* Wait... `parseInt` and `map` are standard right? */
if (parseInt(0.000000005) === 5) { /* Cannot be equal to `0`, it would be too easy. */
if ("1,2" + "3,4" === "1,23,4" /* Great! I can concatenate two strings :) */
&& [1,2] + [3,4] === "1,23,4") { /* ...and two arrays... oh... so useful */
v = 10 * "a";
if (v !== v) { /* My brain hurts right now. */
alert("Next lesson: why you will write `var that = this;` everywhere.");
}
}
}
}
}
}
}
}
}
/*
Joke :)
A value is considered like true when it's different of false, undefined, null, 0, 0.0 and "".
Never use == / != instead of === / !== except for check null and undefined at the same time.
new Array(N) create an array of size N.
new Array(N, M, ...) create an array with the values N, M, ... inside.
array.map send not only one argument to the callback and parseInt can optionally take also the base in argument.
0.000000005 === 5e-9 so parseInt(5e-9) === 5, there is maybe a toString inside... use Math.round instead.
10 * "a" is Not a Number (NaN) and NaN !== NaN is true, maybe you prefer use isNaN instead.
*/
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment