Expressions
Rules
Can be anonymous.
Cannot be hoisted.
File Location
We will be working in the following file:
javascript-library
└── 0-PreWork
└── 1-Fundamentals
└── 1-Grammar-and-Types
└── 2-ControlFlow-and-ErrorHandling
└── 3-Loops
└── 4-Functions
02-expressions.js <----You will be working in this file.
Anonymous Functions
When we say that function expressions can be anonymous, you'll see below that the function does not have a name after the function
keyword is used:
var sayHello = function(){
console.log("Hello");
}
Naming a function expression
You can also put a name on a function expression:
var sayHowdy = function howdy(){
console.log("Howdy");
}
//Printing the value
console.log(sayHowdy());
No Hoisting
Function expressions CAN NOT be hoisted in JavaScript. This means that the call can not come before the function is declared.
//The call - this would cause an error
fooFunction();
//The expression
var foo = function fooFunction() {
console.log('foo');
}
Practice
Practice writing 3 different function expressions of your own choosing.
Last updated