Beautiful JS

JavaScript that belies the common conception of beauty

Sizzle's filters

//...

radio: function( elem ) {
	return elem.nodeName.toLowerCase() === "input" && "radio" === elem.type;
},

checkbox: function( elem ) {
	return elem.nodeName.toLowerCase() === "input" && "checkbox" === elem.type;
},

file: function( elem ) {
	return elem.nodeName.toLowerCase() === "input" && "file" === elem.type;
},

password: function( elem ) {
	return elem.nodeName.toLowerCase() === "input" && "password" === elem.type;
},

image: function( elem ) {
	return elem.nodeName.toLowerCase() === "input" && "image" === elem.type;
},

//...

These methods are formatted in such a way that the string tested against in the second portion of the expression (e.g. "password") is aligned consistently in all methods, meaning more readable code!

The less beautiful alternative would be ... && elem.type === "password" which would mean “password”, “file” and “radio” would be on different columns in the code.

Tiny considerations like this matter!

See these filters on github.

Posted by James on 30 Sep 2011 | linky

Underscore's isNaN method

// Is the given value `NaN`? `NaN` happens to be the only value in JavaScript
// that does not equal itself.
_.isNaN = function(obj) {
    return obj !== obj;
};

See _.isNaN on github.

It’s also worth mentioning Underscore’s annotated source.

Posted by James on 29 Sep 2011 | linky

Subscribe or follow @Beautiful_JS! | Fork then send a pull request to contribute.

Disclaimer: Beautiful code is readable, maintainable and scalable. It's not just nice to look it.