Skip to content

Instantly share code, notes, and snippets.

@CezaryDanielNowak
Last active May 31, 2017 20:13
Show Gist options
  • Save CezaryDanielNowak/6843995 to your computer and use it in GitHub Desktop.
Save CezaryDanielNowak/6843995 to your computer and use it in GitHub Desktop.
Javascript is funny
// run test in you javascript console.
var info = console.log, test = (function() {
var result = function(code) {
return result.codePad(code, eval(code));
},
strPad = function(str, pad) {
var len = str.length;
return len >= pad ? str : Array(pad - len + 1).join(' ') + str;
}
result.codePad = function(a, b) {
info(strPad(a, 42), '\xA0-->\xA0', b);
}
return result;
})();
test('[] == ![]'); // true
test('8 == "08"'); // true
test('[] == 0'); // true
test('null != false'); // true
test('0.2 + 0.1'); // 0.30000000000000004
test('0.7 + 0.1'); // 0.7999999999999999
test(
'var x = 9007199254740992;\n' +
'x + 1 === x'
); // true
test('parseInt(2.002 - 1.002)'); // 0
test('parseInt(0.0000001)'); // 1
test('" \\t\\t\\r\\n " == 0'); // true
test('10 == [[[10]]]'); // true
test('NaN == NaN'); // false
test('({} == {})'); // false
test('[] + []'); // ''
test('{} + []'); // 0
test('{} + {}'); // NaN
test('Math.max(-1, undefined)'); // NaN
test('Math.max(-1, null)'); // 0
test('Math.max(-1, true)'); // 1
test('Math.max(3, [])'); // 3
test('Math.max(-1, [1])'); // 1
test('Math.max(-1, [1, 4])'); // NaN
test('[,,,].join()'); // ',,'
test('"8" + 4'); // 84
test('"8" - 4'); //4
test.codePad('3.toString()', 'Ugly syntax error');
test('3..toString()'); //3
info('Days in Date starts with 1:');
test('new Date("05/31/1989").getDate()'); // 31
info('But months from 0:');
test('new Date("05/31/1989").getMonth()'); // 4
info('And don\'t even expect to get readable data from getYear:');
test('new Date("05/31/2089").getYear()'); // 189
test(
'(function(){\n'+
' return {\n' +
' nice:true\n' +
' }\n' +
'})()'
); // {nice=true}
info('but');
test(
'(function(){\n'+
' return\n' +
' {\n' +
' nice:true\n' +
' }\n' +
'})()'
); // undefined; Allman style won't work for JS http://en.wikipedia.org/wiki/Indent_style#Allman_style
info('RegExp with global flag bahavior is just weird !');
test(
"var regexp = new RegExp('^abcd$', 'g'), result = [];\n" +
"for(var i=4; i--;) {\n" +
" result.push(regexp.test('abcd'));\n" +
"}\n" +
"result;"
); // [true, false, true, false, true]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment