Functional Programming Hoisting

What is hoisting ?

The hoisting is, the browser takes all the declarations such as a,b,c it takes out of the code before showing anything in the browser. It takes these declarations and puts them on top of the website or top of the file that we have up here so I can access them at any point inside the code that I have here.

Declaring a variable and assigning a value is going to affect hoisting because when you load the code inside the browser.

console.log(d): undefined logically console.log should know there is variable d inside the code because the console.log is before the variable d. Inside the console we should not get undefined.

var d; we declared a variable here without assigning a value because that can’t find the variable but there is no value assigned to it

Instead what we should be getting logically a references error that i don’t know this variable is this but we get undefined which means

we have a variable declared inside the document but there is no value assign for variable d thats show when i declared a variable then

the variable taken out of the code console.log can find it variable d because variable is hoisted top of the document.Its pulled up tp top

so console.log knows that there is a variable d.

Because hoisting just takes declaration out and put on the top. It doesn’t take values.

We assigned value in order to see in the console.

d = “fourth” assigned the value before the console.log its still gets the value inside of the browser. Because we declared variable below the console.log and its hoisted. Only declaration can hoisted and we assigned the value before console.log.

console.log(example()). The name function is the same way as variable gets taken out the code and placed it top(hoisted) of the file. We can use it any point inside our code we can use it whenever want inside of the code because its been hoisted

How do we Avoid the hoisting ?

  1. We declared and assign the variable inside the function
  2. we should use anonymous function because name function always gets hoisted in the browser
  3. Hoisting is takes a lot of space in the browser and if we have alotof codes hoisting like above it gets slower to browser.

Anonymous function which we just assign a variable to function and call it.

console.log(a()) -> caught TypeError: a is not a function at index.js. Because anonymous function can’t hoisting.

Hoisting is a term you will not find used in any normative specification prose prior to ECMAScript® 2015 Language Specification. Hoisting was thought up as a general way of thinking about how execution contexts (specifically the creation and execution phases) work in JavaScript. However, the concept can be a little confusing at first.

Conceptually, for example, a strict definition of hoisting suggests that variable and function declarations are physically moved to the top of your code, but this is not in fact what happens. Instead, the variable and function declarations are put into memory during the compile phase, but stay exactly where you typed them in your code.

Technical example

One of the advantages of JavaScript putting function declarations into memory before it executes any code segment is that it allows you to use a function before you declare it in your code. For example:

The above code snippet is how you would expect to write the code for it to work. Now, let’s see what happens when we call the function before we write it:

Even though we call the function in our code first, before the function is written, the code still works. This is because of how context execution works in JavaScript.

Hoisting works well with other data types and variables. The variables can be initialized and used before they are declared.

Only declarations are hoisted

JavaScript only hoists declarations, not initializations. If a variable is declared and initialized after using it, the value will be undefined. For example:

The example below only has initialization. No hoisting happens so trying to read the variable results in ReferenceError exception.

console.log(num); throws ReferenceError exception

num = 6; initialization

Below are more examples demonstrating hoisting.

Example 1

x = 1; Initialize x, and if not already declared, declare it — but no hoisting as there is no var in the statement.

This prints value of y as undefined as JavaScript only hoists declarations.

No hoisting, but since initialization also causes declaration (if not already declared), variables are available.

Functions (and variables defined with ~var~) are put into the memory during the compilation phase.This way these functions can be executed from anywhere even before the actual definition. When we declare the function, the function declaration being read through JS has two phases: first compilation and second execution.

Function declaration is hoisting meaning the code is parsed at its elevated top of the page,It means you can call the function above. Before it’s defined, that doesn’t work for function expression.



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
Soner Mezgitci

Soner Mezgitci


Software Engineer | Ruby on Rails | JavaScript | HTML5 | CSS | PostgreSQL | React | Redux