In contrast to Flanagan's view, I believe that the comma is an incredibly valuable tool that enhances the readability and elegance of code, particularly for those with experience:
For a comprehensive examination of comma usage, check out this detailed article:
Here are some examples to illustrate its effectiveness:
function renderGraph() {
for(var x = 1, y = 10; x*y; x++, y--) {
console.log(new Array(x*y).join('*'));
}
}
An example of a Fibonacci sequence generator:
for (
var i=2, sequence=[0,1];
i<15;
sequence.push(sequence[i-1] + sequence[i-2]), i++
);
// 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377
Creating a function to find the first parent element, similar to jQuery's .parent()
function:
function findAncestor(element, tag) {
while(element = element.parentNode, element && (element.tagName != tag.toUpperCase()));
return element;
}
//element in http://ecma262-5.com/ELS5_HTML.htm
var element = $('Section_15.1.1.2');
findAncestor(element, 'div'); //<div class="page">