The difference between function, var, and let/const.

function myFunc(){}
var myFunc = function() {};
const myFunc = () => {};

function 🔑

The function keyword will hoist your variable and its value. Even if your function is defined at the end of your document, it might as well have been defined at the beginning for all intents and purposes.

console.log(add(1, 2)); // 3function add(a, b) {
return a + b;
}

var 👛

The var keyword hoists the variable, but not the value. What does it mean to hoist a variable but not its value? Let’s take a look at undeclared variables.

console.log(add); // Uncaught ReferenceError: add is not defined
console.log(add); // undefined
var add = function(a, b) {
return a + b;
};
console.log(add); // Function
let add;
console.log(add); // undefined
add = function(a, b) {
return a + b;
};
console.log(add); // Function

let/const 🥇

Unlike function and var, the let and const keywords do not hoist at all!

console.log(add); // Uncaught ReferenceError: add is not defined
const add = function(a, b) {
return a + b;
};
console.log(add); // Function

Conclusion 🔚

If you have any questions or great commentary, please leave them in the comments below.

Senior front end engineer / charlesstover.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store