Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created October 24, 2014 21:33
Show Gist options
  • Save KamilLelonek/a4b8a27cbfb766b0b5ac to your computer and use it in GitHub Desktop.
Save KamilLelonek/a4b8a27cbfb766b0b5ac to your computer and use it in GitHub Desktop.
Hipster JavaScript usages
// Boring
if (success) {
obj.start();
} else {
obj.stop();
}
// Hipster-fun
var method = (success ? 'start' : 'stop');
obj[method]();
// Or even saver with two conditions
success ? obj.start() : obj.stop();
///////////////////////////////////////////////////////
// default to 'No name' when myName is empty (or null, or undefined)
var name = myName || 'No name';
// make sure we have an options object
var doStuff = function(options) {
options = options || {};
// ...
};
///////////////////////////////////////////////////////
// Boring
if (isThisAwesome) {
alert('yes'); // it's not
}
// Awesome
isThisAwesome && alert('yes');
// Also cool for guarding your code
var aCoolFunction = undefined;
aCoolFunction && aCoolFunction(); // won't run nor crash
///////////////////////////////////////////////////////
console.time('test');
console.timeEnd('test');
///////////////////////////////////////////////////////
function checkIfNumberIsOdd(number) {
return !!(number & 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment