Hidden Features of JavaScript? [closed]
You don't need to define any parameters for a function. You can just use the function's arguments
array-like object.
function sum() {
var retval = 0;
for (var i = 0, len = arguments.length; i < len; ++i) {
retval += arguments[i];
}
return retval;
}
sum(1, 2, 3) // returns 6
I could quote most of Douglas Crockford's excellent book JavaScript: The Good Parts.
But I'll take just one for you, always use ===
and !==
instead of ==
and !=
alert('' == '0'); //false
alert(0 == ''); // true
alert(0 =='0'); // true
==
is not transitive. If you use ===
it would give false for
all of these statements as expected.