Skip to content

Instantly share code, notes, and snippets.

@jescalan
Created March 9, 2013 18:21
Show Gist options
  • Select an option

  • Save jescalan/5125130 to your computer and use it in GitHub Desktop.

Select an option

Save jescalan/5125130 to your computer and use it in GitHub Desktop.
Javascript function declaration
// In this first example, I declare a function with what I call
// the 'standard' syntax, which is what I have taught. Because of
// this, I can make a call to the function before or after the declaration.
say_hello(); // logs 'hello world!'
function say_hello(){
console.log('hello world!');
}
say_hello(); // logs 'hello world!'
// In the second example here, I create a function by assigning an
// anonymous function to a variable, then making a call on the variable.
// This will throw an error unless the variable was defined in the right order.
say_hello2(); // error: 'say_hello' is undefined
var say_hello2 = function(){
console.log('hello world!');
}
say_hello2(); // logs 'hello world!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment