Here are some good one liners in javascript
//Hello World Program Which is Palindrome (alert)('Hello, World') && ('dlroW ,olleH')(trela) //Reverse a string "this is a string".split("").reverse().join(""); //Trim a string " Hello World ".replace(/^\s*|\s*$/g,'') //HTML escape "<b>Hello HTML</b>".replace(/([&<>])/g,function (c) {return "&" + {"&": "amp","<": "lt",">": "gt"}[c] + ";";}); //Shuffle an array [1,2,3,4,5].sort(function(){return (4*Math.random()>2)?1:-1}) //Detect IE isIE='\v'=='v'; //Force String to be a Number (multiply or divide by 1) "100"*1 //Force to be boolean !!"some value" //Converting string to Leet "Hello Hacker".replace(/[a-z]/g,function f(a){return "4BCD3F6H1JKLMN0PQR57"[parseInt(a, 36)-10] || a.replace(/[a-t]/gi,f)}); //Clone an array var clone = [1,2,4,5,6,9].slice(0);
Interesting... when I first saw the palindrome, I thought, how is ('dlroW ,olleH')(trela) supposed to work?? trela is undefined, and how can you apply a string as a function?
ReplyDeleteBut apparently alert() returns undefined, which is falsy, and so ('dlroW ,olleH')(trela) is never executed... and therefore never throws an error. I guess that's due to JS being interpreted rather than compiled, so what minimal checking there is, doesn't happen until an expression is evaluated.