Hosting - mikehov/Dating-app GitHub Wiki

Hoisting Research

Mike Hovenier | Tech-4

What is Hoisting

In Javascript a variable can be declared after is has been used. So a variable can be used before it has been declared. This is something you can do with VAR. For Let and Constthis does not work. They need to be declared before you can use them.

So for example:

console.log(example());

function example() {
  var a = 10;
  return a;
}

This function is named function, the console will show "10". This is because the function is Hoisted. This can be really useful so you can use your answers anywhere. But the downside is that your browser will load a little bit slower. Also a downside is that it can screw up your structure, because in some scenerios it doesn't matter anymore where you declare a function or var. You always want structure that's accessiable and reusable. So you don't want to host code in the browser.

There is a difference between a anonymous function and named function. This is function that doesn't have a name, that's what they call an anonymous function, so for example:

var = anonymousFunction function() {
  var a = 10;
  return a;
}

console.log(anonymousFunction());

This code above will work, but let's say you put the 'console.log(anonymousFunction());' above the anoymous function this will not work and you will get a error. So this is something you want to be using cause it organizes your code more and keeping your code structure.

Source

w3schools. (n.d.). JavaScript Hoisting. Retrieved May 28, 2020, from https://www.w3schools.com/js/js_hoisting.asp

mmtuts. (2018, June 4). 18: Hoisting in JavaScript Explained | What is Hoisting in JavaScript | JavaScript Tutorial [Video file]. YouTube. Retrieved from https://www.youtube.com/watch?v=ppMlvGMT2qE