On Github gmdayley / jspuzzlers
alert("A");
var x = parseInt("5"); var y = parseInt("05"); var z = parseInt("08"); alert((x + y) * z);
parseInt("08", 10) // 8
alert([ typeof(NaN), typeof(Number(123)), typeof(null), typeof([1,2,3]) ].join());
var toType = function(obj) { return ({}).toString.call(obj) .match(/\s([a-zA-Z]+)/)[1].toLowerCase() }
var $2 = 0.2; var $4 = 0.2 + 0.2; var $6 = 0.2 + 0.2 + 0.2; var $8 = 0.2 + 0.2 + 0.2 + 0.2; if ($4 !== .4) { alert("A"); } else if ($6 !== .6) { alert("B"); } else if ($8 !== .8) { alert("C"); } else { alert("D"); }
0.0 === 0; //TRUE, unlike Java or C#
A. var rounded = Math.round(num); B. var rounded = ~~ (0.5 + num); C. var rounded = ~~ (num + (num > 0 ? .5 : -.5)); D. var rounded = (0.5 + num) << 0;
var ee = new (require('events').EventEmitter); var die = false; ee.on('die', function() { die = true; }); setTimeout(function() { ee.emit('die'); }, 100); while (!die) {} console.log('done');
if (Math.min() < Math.max()) { alert('Of course'); } else { alert('Yikes'); }
//One way to find it var x = Math.pow(2, 53) //9007199254740092 x === x + 1 //true //Better way Number.MAX_VALUE; Number.MIN_VALUE;
for(var i = 0; i < 3; i++) { setTimeout(function() { console.log(i); }, 0); }
for(var i = 0; i < 3; i++) { (function(x) { setTimeout(function() { console.log(x); }, 0); })(i); }
var a = [...]; A. for (var i = 0; i < a.len; i++) { ... }; B. var i = a.length; while (i--) { ... }; C. for (var i in a) { ... }; D. a.forEach(function(i) { ... });
for (var i = 0, len = a.len; i < len; i++) { ... };
var x = 2; var y = 3; (function (){ alert(x + y); x = 0; var y = 2; })();
var x = 2; var y = 3; (function (){ var y; alert(x + y); x = 0; y = 2; })();
function fn(length, width) { this.length = length; this.width = width; } alert(fn.length + new fn(3, 4).length);
function fn(l, w) { this.length = l; this.width = w; } alert(fn.length + new fn(3, 4).length);
var background = function() { return { color : 'blue' }; }; alert(background().color);
var background = function() { return; //Thanks, but no thanks! { color : 'blue' }; }So, instead of returning the object, undefined is returned, causing a TypeError to be thrown
var a = new Array(1, 2, 3); var b = new Array(4, 5); var c = new Array(6); var d = a.concat(b, c); alert(d.toString());
var x = new Array(3); //Produces this [undefined, undefined, undefined].join(); //Which looks like this "" + String(undefined) + "," + String(undefined) + "," + String(undefined); //Resulting in ',,' === x; //true
alert(+[]+ +'0XE'+ +true+ +'1E1');
+new Date
var a = 0; var b = 0 * -1; if (a === b) { if (1 / a === 1 / b) { alert("A"); } else { alert("B"); } } else { alert("D"); }
var user = { name: 'Mike', hello: function(thing) { alert(this + " says hello " + thing); } } user.hello('World');
var user = { name: 'Mike', hello: function(thing) { alert(this + " says hello " + thing); } } user.hello('World');
var user = { name: 'Mike', hello: function(thing) { alert(this + " says hello " + thing); } } function pass(fn) { fn('World'); } pass(user.hello);
var user = { name: 'Mike', hello: function(thing) { return (this + " says hello " + thing); } } function pass(fn) { fn('World'); } pass(user.hello);
var user = { name: 'Mike', hello: function(thing) { alert(this + " says hello " + thing); } } user.hello.call('World');
var user = { name: 'Mike', hello: function(thing) { alert(this + " says hello " + thing); } } user.hello.call('World');
function Playlist(name) { this.name = name; this.songs = []; this.addSong = function(id, song) { this.songs[id] = song; } this.showPlaylist = function() { alert(this.songs.join(',')); } }
var plist = new Playlist("90s"); plist.addSong('311', 'Beautiful Disaster'); plist.addSong('join', 'Scratch'); plist.addSong('U2', 'Numb'); plist.showPlaylist();
this.songs = {};
alert(++[[]][+[]]+[+[]]);
alert(++[[]][+[]]+[+[]]);
alert(++[[]][+[]]+[+[]]); => alert(++[[]][0]+[0]);
function Book(title){ this.title = title; return { title: 'JavaScript: The Good Parts'}; } function ZipCode(zip){ this.zip = zip; return 84043; }
var book = new Book('Twilight'); var zip = new ZipCode(07849); if(book instanceof Book){ alert('Book')}; if(zip instanceof ZipCode){ alert('ZipCode')};
function Foo() { var privateVar = 'World'; function privateMethod() {} return { publicVar : 'Hello', getString : function() { return publicVar + ' ' + privateVar; } }; }However, you can not use instanceof to check if its of type Foo
if("1" == 1) { if("" == 0) { if("" == "0") alert("A"); else alert("B"); } else alert("C"); } else alert("D");
"1" === 1 // false Number("5") === 5 // true
var x = 'globalX', y = 'globalY'; function outer() { this.x = 'funcX'; var y = 'funcY'; this.inner = function() { alert(x + ":" + y); } } new outer().inner();
var x = 'globalX', y = 'globalY'; function outer() { this.x = 'funcX'; var y = 'funcY'; this.inner = function() { alert(x + ":" + y); } } new outer().inner();