These are some of the weirdest things brought to us by JavaScript.
- Number
- Math
- Operators
- String
- Array
- Immediately Invoked Function Expression
- Lexical Grammar
- Statements
- Destructuring
- Temporary Dead Zones
Number('0.') // 0
Number('.0') // 0
Number('.') // NaN
Number({}) // NaN
Number([]) // 0
Number(undefined) // NaN
Number(null) // 0
Number('0O0') // 0
Number('0X0') // 0
Number.MAX_VALUE > 0; //true
Number.MIN_VALUE < 0; //false
42.toFixed(2) // SyntaxError: Invalid or unexpected token
42. toFixed(2) // SyntaxError: Unexpected identifier
42 .toFixed(2) // '42.00'
42 . toFixed(2) // '42.00'
42.0.toFixed(2) // '42.00'
42..toFixed(2) // '42.00'
(42).toFixed(2) // '42.00'
Math.max() > Math.min() // false
[] + {} // '[object Object]'
{} + [] // 0
true + true === 2 // true
true - true === 0 // true
true === 1 // false
0.1 + 0.2 === 0.3 // false
1 < 2 < 3 // true
3 > 2 > 1 // false
'5' + 3 // '53'
'5' - 3 // 2
String(-0) // '0'
Number('-0') // -0
String(null) // 'null'
String([null]) // ''
String(undefined) // 'undefined'
String([undefined]) // ''
String({}) // '[object Object]'
String([]) // ''
var s = Symbol('Hello World') // Symbol(Hello World)
String(s) // 'Symbol(Hello World)'
s + '' // TypeError: Cannot convert a Symbol value to a string
parseInt('15') // 15
parseInt('15 and more') // 15
parseInt('more and 15') // NaN
(!+[]+[]+![]).length // 9
[] == ![] // true
Array.apply(null, Array(3)) // [ undefined, undefined, undefined ]
Array.apply(null, [,,,]) // [ undefined, undefined, undefined ]
Array.apply(null, {length : 3}) // [ undefined, undefined, undefined ]
Array.apply(null, (a,b,c) => {}) // [ undefined, undefined, undefined ]
[10, 9, 8, 3, 2, 1, 0].sort() // [ 0, 1, 10, 2, 3, 8, 9 ]
function () { console.log('Hello World') }() // SyntaxError: Unexpected token (
function foo() { console.log('Hello World') }() // SyntaxError: Unexpected token )
!function () { console.log('Hello World') }() // Hello World
function foo() {
return
'Hello World';
}
foo() // undefined
switch (42) {
default:
console.log('foo');
case 10:
case 20:
console.log('bar');
break;
case 30:
console.log('zed');
break;
}
// foo
// bar
function foo() {
try {
return 2
} finally {
return 3
}
}
foo() // 3
function foo(x = {y: 10}, {y = 20} = {}) {
console.log(x.y, y);
}
foo(); // 10 20
foo({y: 30}, {y: 40}); // 30 40
foo({}, {}); // undefined 20
(function(a = b, b) {
}(undefined, 1)); // ReferenceError
let b = 1;
(function(a = b, b) {
console.log(a, b);
}(undefined, 2)); // ReferenceError
{
typeof a; // undefined
typeof b; // ReferenceError
let b;
}