Javascript Language Sugar (one liners)


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);