definition() //'I am definition'
expression() // Uncaught TypeError: expression is not a function
function definition() {
return 'I am definition'
}
var expression = function() {
return 'I am expression'
}Expressions result in a value even if just “undefined” Definition creates sort of like a reference but it’s not an actual value so you can’t run it
function definition() {
return 'I am definition'
}
let expression
definition()
expression()
expression = function() {
return 'I am expression'
}function foo() {
// i pity this code
}();In fact, this is how JS reading IIFE (hoisting applied)
function foo() {
}
(); // as if you wrote it on separate linesYou can make it work simply by putting parentheses around it. In this way, you can make the function at an expression.
;(function foo() {})()Why would I ever use one? => To control variable scope
;(function foo() {})()
console.log(foo) // referenceError: foo is not definedThe variables inside of the foo function will only be availble inside the function not outside of the function, which can let us use dumb(?) and plain naming conventions in our application.