Hoisting

Hoisting

Use Functions Before Declarations

Table of contents

No heading

No headings in the article.

Javascript preassumes that the declaration is on the top of the current scope.

Let's say we have defined a function at the bottom of our scope of script or function, even a javascript interpreter runs line by line, everyone can use this function before declaration. But keep in mind we will declare it later after using it, otherwise, it will never work

print("hello")

function print(name){
console.log(name)
 }

Output

hello

This will print hello, no matter whether you declare it before calling or after calling.


console.log(s)
var s = 7
console.log("answer after declaration")
console.log(s)

Output

undefined
answer after declaration
7

In short, we can say Hoisting is a behaviour that moves the declarations on the top while interpreting its code.

Arrow functions are not hoisted.

func(3)

let func=(variable)=> console.log(variable)

The above code will return ReferenceError


Do it yourself with var keyword and ``` arrow functions


Also comment down if I have committed any mistake.
<br>
Let's connect on my socials -- <br>
Always Open for new opportunities ,if I am free :P <br>
[Twitter](https://twitter.com/xauravww)| [LinkedIn](https://www.linkedin.com/in/itsmesaurav/) |
[Showwcase](https://www.showwcase.com/xauravww)