Member-only story
function expressions vs function declarations in JavaScript
function expressions vs function declaration — #7 of the 40 Essentials of JavaScript
Welcome back to the JavaScript Essentials Series, this article attempts to explain difference between function expression and function declaration.
Function expressions and function declarations are two different ways for a programmer to create a function, however, how they are declared and executed differ greatly.
Function Expressions
Function expressions allow you to create functions within an expression. This means that the function is part of a larger expression and the body of the function cannot stand alone. Function expressions are always named or anonymous, meaning that the function can either have a name that is associated with it or it can be anonymous without being called.
Lets take a look on some examples of function expression:
// Assigning a function to the variable add:
const add = function(a, b) { return a + b };
// Immediately invoked function expressions (IIFE).
function(){console.log("Hello World")}) ();
// Passing a function as an argument:
arr.filter(function(item) { return item < 10});