Skip to content

Instantly share code, notes, and snippets.

@koenbok
Created May 30, 2014 13:59
Show Gist options
  • Select an option

  • Save koenbok/bc2025785c79ef5c4d8a to your computer and use it in GitHub Desktop.

Select an option

Save koenbok/bc2025785c79ef5c4d8a to your computer and use it in GitHub Desktop.
Learn Coffee Script

Learn CoffeeScript in an afternoon. Or use it as a cheat sheet.

########################################################################

Part 1: Variables

A variable is a way to store something under a name. It's pretty simple:

myVariable = 1

From this point on the variable myVariable contains 1. You can change this at any point just by assigning a new value. If you ask the value of a variable before it has one (defined) it returns a special value 'undefined'.

myUnexistingVariable # Returns 'undefined'

#Variables can store different types of data, see below.

########################################################################

Part 2: Data types

This is a boolean

A boolean can only have two values: false or true

myBoolA = true myBoolB = false

This is a number

Numbers can be small, big, rounded, positive or negative

myNumberA = 1 myNumberB = 55.0 myNumberC = -223894

You can calculate with numbers like this (operators)

myNumberA + myNumberB # Output: 66.0 myNumberA / myNumberB # Divide myNumberA * myNumberB # Product myNumberA ** myNumberB # Power

This is a string

A string is a set of characters like words or complete sentences

myStringA = "Hello there"

You can combine strings like this:

myStringB = "#{myStringA}, I am the dude"

Output: "Hello there, I am the dude"

But strings can also hold complex values you cannot describe in pure numbers

myStringC = "rgba(255,0,0,0.1)" myStringD = "1px solid #FFFFFF"

This is a list

Lists contain an ordered set of values. The values can be any type. They can even be nested (list of lists).

myListA = [1, 2, 3, 4, 5] # Just numbers myListB = [1, "test", 22, [1, 2, 3]] # Mixed

You can access any item in a list by it's number (it's called an index) which starts at 0

myListA[0] # Output: 1 myListA[4] # Output: 5

You can also count the items in a list

myListB.length # Output: 4

Or grab a range from the list

myListB[0..2] # Output: [1, 2, 3]

This is an object

Objects are a bit like lists, but instead of by number, you can access them by a string (word). Much like a dictionary from a phone book (look up a person by last name). We call the 'lookup string' a key.

myObjectA = {name: "Framer", age:12}

You can also write objects like this, it's the exact same thing as above:

myObjectB = name: "Framer" age: 12

To find a value in an object you use the key

myObjectB["name"] # Output: "Framer"

If you use a key that does not exist, you get the special undefined value

myObjectB["skdjslkdj"] # Output: undefined

Once you have made an object, you can add more stuff in it like this:

myObjectB["platform"] = "Mac"

Or remove something like this:

delete myObjectB["platform"] myObjectB["platform"] # Output: undefined

########################################################################

Part 2: Functions

Functions are bits of code that you can execute. It's pretty much the same thing as a function in mathematics. They take input and generate output. The part in between is just a description how to get from one to the other. When you 'run' a function for a specific set of inputs, programmers say you 'call' the function, and the input(s) are 'arguments'. Let's start with a simple example:

This is how you define a function (note it follows the standard way of defining variables)

myFunction = (input) -> output = input * 2 return output

To now use (or call) the function with the number 42 (argument) you do this:

myFunction(42) # Output: 84

You can assign the output of a function directly to a new variable

myCalculatedNumber = myFunction(42) myCalculatedNumber # Output: 84

Some examples of what you use functions for are:

- Describe to do something, but use it later (when it gets called)

- Wrap something you do multiple multiple times in a function so you can re-use it easily

########################################################################

Part 3: Comparators (or logical operators)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment