Created
August 1, 2017 22:24
-
-
Save petrucci34/4b8917c3765a47ad08e598598716092b to your computer and use it in GitHub Desktop.
JavaScript Oddities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Lack of function signature. | |
var foo = 'im a number' | |
function divideByFour(number) { | |
return number / 4 | |
} | |
console.log(divideByFour(foo)) //NaN | |
// Immutability support. | |
const array = [3, 6] | |
array[5] = 9 | |
console.log(array) // [ 3, 6, , , , 9 ] | |
// Arrays don't have to contain same type of objects. | |
var array = [0, 1, 2] | |
array["hello"] = "world" | |
console.log(array) // [ 0, 1, 2, hello: 'world' ] | |
// Documentation suggests: It’s best to leave exceptions as a last line of defense, | |
// for handling the exceptional errors you can’t anticipate, and to manage anticipated | |
// errors with control flow statements. | |
function tenDividedBy(number) { | |
if (number == 0) { | |
throw "can't divide by zero" | |
} | |
return 10 / number | |
} | |
console.log(tenDividedBy(0)) | |
/* | |
/temp/file.js:9 | |
throw "can't divide by zero" | |
^ | |
can't divide by zero | |
*/ | |
// No support for decimals. | |
console.log(0.1 + 0.2) //0.30000000000000004 | |
console.log(0.1 + 0.2 === 0.3) //false | |
// Confusing math operations. | |
var a = 0 | |
var b = -0 | |
console.log(a === b) // true | |
console.log(1/a === 1/b) // false | |
var x = Math.sqrt(-2) | |
console.log(x === NaN) //false | |
console.log(isNaN(x)) //true | |
console.log(isNaN('hello world')) //true | |
// Treatment of null and undefined is confusing: | |
var foo; | |
foo === null; //false | |
foo === undefined; //true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment