Expression closures (-8 to 10 chars)
function(x){return x*x;} // 1.7
function(x)x*x // 1.8
// 1
// 0....5....0
for each
Assuming no one extends Array.prototype,
var S=0,a=[1,2,3,4,5];for(i=0;i<a.length;++i)S+=a[i]*a[i]; // 1.5
var S=0,a=[1,2,3,4,5];for(i in a)S+=a[i]*a[i]; // 1.5
var S=0;for each(y in[1,2,3,4,5])S+=y*y; // 1.6
var S=[1,2,3,4,5].reduce(function(x,y)x+y*y); // 1.8
// 1 1 1
// 0....56...0....5..8
Array comprehensions (-lots of chars)
var I=[0,2,-5,6,-72,44];
var O=[];for(i=0;i<I.length;++i)if(I[i]>0)O.push(I[i]); // 1.5
var O=[];for(i in I)if(I[i]>0)O.push(I[i]); // 1.5
var O=[];for each(x in I)if(x>0)O.push(x); // 1.6
var O=[x for each(x in I)if(x>0)]; // 1.7
// 1 1 22
// 0....5..890....5....01
Destructuring assignment
t=A;A=B;B=t; // 1.5
[A,B]=[B,A]; // 1.7
m=/<a\s+href=(['"])(.+?)\1>/.exec(src);s=m[0],l=m[2]; // 1.5
[s,,l]=/<a\s+href=(['"])(.+?)\1>/.exec(src); // 1.7 '
// 0....5...9
Misc
- Many functions once available only to String or Array is now usable with all objects in 1.6 via the syntax
String.method(object, args)
.
- Array.some, Array.every, Array.forEach, Array.indexOf and Array.lastIndexOf.
Ref: