Skip to content

Instantly share code, notes, and snippets.

@celeron55
Created July 17, 2012 17:52
Show Gist options
  • Save celeron55/3130819 to your computer and use it in GitHub Desktop.
Save celeron55/3130819 to your computer and use it in GitHub Desktop.
// Assignment:
var number = 42
var opposite = true
// Conditions:
number = opposite ? -42 : number
// Functions:
var square = function(x){ return x * x }
// Arrays:
var list = [1, 2, 3, 4, 5]
// Objects:
var math = {
root: Math.sqrt,
square: square,
cube: function(x){ return x * square(x) }
};
// Splats:
// This is so rare nobody cares of a few lines of code.
var race = function() {
var winner = arguments[0]
var runners = arguments.length >= 2 ? [].slice.call(arguments, 1) : []
return print(winner, runners)
};
// Existence: Oh hell, why not just define the variable so you can use it like a variable?
if(typeof elvis !== "undefined" && elvis !== null) alert("I knew it!")
// Array comprehensions (JS 1.6):
var cubes = list.map(function(x){ return math.cube(x) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment