Skip to content

Instantly share code, notes, and snippets.

@founddrama
Created November 21, 2010 15:33
Show Gist options
  • Save founddrama/708820 to your computer and use it in GitHub Desktop.
Save founddrama/708820 to your computer and use it in GitHub Desktop.
why you should basically never use the Number constructor
var n = new Number(x); // where x is a global storing an incrementer
function doMath(a){
if (typeof a == "number") {
return a + x;
}
}
doMath(n);
var n = new Number(1);
alert(n); // 1
typeof n; // object
n instanceof Number; // true
var nn = 1;
alert(nn); // 1
typeof nn; // number
n instanceof Number; // false
n == nn; // true
typeof n == typeof nn; // false
n++;
nn++;
alert(n); // 2
alert(nn); // 2
n == nn; // true
typeof n; // number
typeof nn; // number
typeof n == typeof nn; // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment