#jsquizz

Fri, 04 Dec 2009

Recently I started to ask twitterverse JavaScript related questions, just for fun. I called it “jsquizz”, but twitter is very fast media, so I decided to copy some of this questions here, so I could easily find them later.

var date = +new Date;
// much shorter than new Date().getTime()
var a = "</script>"; // wouldn’t work
var a = "</s" + "cript>"; // usual way to workaround
var a = unescape("<%2fscript>");
// unusual way to workaround
var a = "<\/script>"; // shorter way
var a = 120000; // make it shorter?
var a = 12e4;
var a = Math.floor(b);
// if b is always positive, we could make it shorter
var a = ~~b;
var a = new Boolean(false); // how to quickly check if (a)?
alert(typeof a); // "object"
alert(!!a); // always true, because a is an object
alert(!!+a);
// right result, because “+” is calling valueOf
// method and convert result to number “0..1”
alert(a>0); // even shorter
[0, 1, 2][0, 1, 2] // what the result of this expression?
alert([0, 1, 2][0, 1, 2]); // 2
// first brackets is array declaration,
// second brackets contains comma
// operator, so it is almost equivalent to
alert([0, 1, 2][2]); // 2