Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active December 4, 2019 08:12
Show Gist options
  • Save vyspiansky/71b9055713570d4b1760 to your computer and use it in GitHub Desktop.
Save vyspiansky/71b9055713570d4b1760 to your computer and use it in GitHub Desktop.
JavaScript: oddities
/*
* Source: http://goo.gl/uCbje5
* Author: Eugene Zlobin
* Author Google+ Profile: http://goo.gl/REGj9K
*/
'5' + 4 // 54
5 + '4' // 54
5 + + '4' // 9
'5' * '2' // 10
'2px' + 4 // 2px4
// ------------------------
[1, 2, 3] + 1 // 1,2,31
'$' + 5 + 4 // $54
3 / 0 // Infinity
12 - 'px' // NaN
// ------------------------
var foo = '1';
var bar = '2';
foo -= bar -= ' is string';
// foo = NaN
// bar = NaN
// ------------------------
var foo = 5.44444;
~~foo; // 5
// ------------------------
null == undefined // true
null === undefined // false
void 0 // undefined
NaN === NaN // false
3 > 2 > 0 // true
3 > 2 > 1 // false
// ------------------------
7 && 2 // 2
2 && 7 // 7
0 && 2 // 0
-1 && 2 // 2
(1, 2, 3, 4, 5) // 5
// ------------------------
typeof null // object
null instanceof Object // false
typeof NaN // number
typeof [1,2] // object
typeof(3) // number
isFinite(undefined) // false
isFinite(null) // true
null == false // false
!null // true
// ------------------------
3.toString() // SyntaxError: Unexpected token ILLEGAL
3 .toString() // "3"
3..toString() // "3"
(3).toString() // "3"
// ------------------------
[1, 2] instance of array // true
[1, 2] instance of object // true
// ------------------------
var f = function() {};
f.call('test'); // String {0: "t", 1: "e", 2: "s", 3: "t", length: 4}
// ------------------------
var fn = function() {
retutn this;
};
fn.call(null); // window
// ------------------------
var Array = 100;
var foo = new Array([2,3,4].length); // TypeError: number is not a function
// ------------------------
alert(111111111111111111111) // alerts 111111111111111110000
// ------------------------
var x = [typeof 5, typeof null][1] // x === 'string'
// ------------------------
// Singleton pattern
var isSing = {
foo: 1,
fn: function() {}
};
var isSingTwo = (function() {
var i = 0,
bar = 'test';
return {
getBar: function() {
return bar;
}
};
}());
const isSingThree = (function() {
var Constr = function() {
this.foo = 'test';
},
instance = null;
return {
getInstance: function() {
return instance || (instance = new Constr);
}
};
}());
// ------------------------
// minimum and maximum element of array
var foo = [1, 2, 3];
Math.min(foo); // NaN
Math.min.apply(Math, foo); // 1
Math.max.apply(Math, foo); // 3
// ------------------------
// array merging
var foo = [1, 2, 3];
foo.push.apply(foo, [4, 5, 6]);
// or
var foo = [4, 5, 6]
bar = [1, 2, 3].concat(foo); // 1, 2, 3, 4, 5, 6
// ------------------------
// clone Array
var foo = [1, 2, 3];
var bar = foo.slice(0); // bar = [1, 2, 3]
@isthisstackoverflow
Copy link

[10, 5, 2].sort(); // [10, 2, 5]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment