for-in
loops are often incorrectly used to loop over the elements in an Array
. This is however very error prone because it does not loop from 0
to length - 1
but over all the present keys in the object and its prototype chain. Here are a few cases where it fails:
function printArray(arr) { for (var key in arr) { print(arr[key]); } } printArray([0,1,2,3]); // This works. var a = new Array(10); printArray(a); // This is wrong. a = document.getElementsByTagName('*'); printArray(a); // This is wrong. a = [0,1,2,3]; a.buhu = 'wine'; printArray(a); // This is wrong again. a = new Array; a[3] = 3; printArray(a); // This is wrong again.
Always use normal for loops when using arrays.
function printArray(arr) { var l = arr.length; for (var i = 0; i < l; i++) { print(arr[i]); } }
The following are all false in boolean expressions:
null
undefined
''
the empty string0
the number
But be careful, because these are all true:
'0'
the string[]
the empty array{}
the empty object
This means that instead of this:
while (x != null) {
you can write this shorter code (as long as you don't expect x to be 0, or the empty string, or false):
while (x) {
And if you want to check a string to see if it is null or empty, you could do this:
if (y != null && y != '') {
But this is shorter and nicer:
if (y) {
Caution: There are many unintuitive things about boolean expressions. Here are some of them:
Boolean('0') == true
'0' != true0 != null
0 == []
0 == falseBoolean(null) == false
null != true
null != falseBoolean(undefined) == false
undefined != true
undefined != falseBoolean([]) == true
[] != true
[] == falseBoolean({}) == true
{} != true
{} != false
Conditional (Ternary) Operator (?:)
Instead of this:
if (val != 0) { return foo(); } else { return bar(); }
you can write this:
return val ? foo() : bar();
It is common to see this:
function listHtml(items) { var html = '<div class="foo">'; for (var i = 0; i < items.length; ++i) { if (i > 0) { html += ', '; } html += itemHtml(items[i]); } html += '</div>'; return html; }
but this is slow in Internet Explorer, so it is better to do this:
function listHtml(items) { var html = []; for (var i = 0; i < items.length; ++i) { html[i] = itemHtml(items[i]); } return '<div class="foo">' + html.join(', ') + '</div>'; }
You can also use an array as a stringbuilder, and convert it into a string with myArray.join('')
. Note that since assigning values to an array is faster than using push()
you should use assignment where possible.
Node lists are often implemented as node iterators with a filter. This means that getting a property like length is O(n), and iterating over the list by re-checking the length will be O(n^2).
var paragraphs = document.getElementsByTagName('p'); for (var i = 0; i < paragraphs.length; i++) { doSomething(paragraphs[i]); }
It is better to do this instead:
var paragraphs = document.getElementsByTagName('p'); for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) { doSomething(paragraph); }
This works well for all collections and arrays as long as the array does not contain things that are treated as boolean false.
In cases where you are iterating over the childNodes you can also use the firstChild and nextSibling properties.
var parentNode = document.getElementById('foo'); for (var child = parentNode.firstChild; child; child = child.nextSibling) { doSomething(child); }