JavaScript Concept 5 - Statement(Definition) and Expression

The difference on the usage of ‘Definition’ and ‘Expression’

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'
}

Explain why the following doesn’t work as IIFE

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 lines

You 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 defined

The 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.


RossenaHuh
Written by@RossenaHuh
Fast learning Web Developer with interests in state-of-art technologies & a person who continuously seeks for diversity and better communications in life.

GitHub