Skip to content

Instantly share code, notes, and snippets.

@LucienLee
Last active August 6, 2021 07:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LucienLee/870b170d39060145aadb796309ec9797 to your computer and use it in GitHub Desktop.
Save LucienLee/870b170d39060145aadb796309ec9797 to your computer and use it in GitHub Desktop.
JavaScript Anomalies
/* Closure */
const buttons = document.getElementsByClassName('three-buttons');
for (var i = 0; i < 3; i++) {
buttons[i].addEventListener(function(){
console.log(i)
});
}
// Click 1st button -> 2
// Click 2nd button -> 2
// Click 3rd button -> 2
/* Type */
console.log(NaN === NaN);
// -> false
console.log([1,2,3] == [1,2,3]);
// -> false
/* Prototype */
Array.prototype.push('😜');
let arr = [];
console.log(arr.length)
// -> 0
console.log(arr[0])
// -> '😜'
/* Arguments is not an array*/
(function test() {
arguments.map(arg => arg*2)
// -> arguments.map is not a function
Array.from(arguments).map(arg => arg*2)
})(1, 2, 3, 4)
/* float vs int toString */
console.log(2.toString());
// -> SyntaxError
console.log(2..toString());
// -> '2'
/* Object use string as key */
var a={},
b={foo: 1},
c={bar: 2};
a[b]=123;
a[c]=456;
console.log(a[b]);
// -> 456 (a['object object'] = 456)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment