Variable Hoisting in JavaScript

--

Hoisting is a default behaviour in JavaScript where all the declarations (variables & functions) are moved to the top of the current scope. I’m gonna discuss on variable hoisting today.

Let’s understand the variable hoisting with the below sample.

a = "Hello";console.log(a); // Hello var a;

We can see “Hello” on the console even though we declared ‘a’ in the end. Well, This is where hoisting comes into play. By default, JavaScript moves the declarations at the top while execution, and it is initialized. This is ‘Variable Hoisting’

Let’s look into another example.

console.log(a); //undefinedvar a = "Hello";

We see “undefined” in the console. But Why ?

This is because by default JavaScript moves only the declaration at the top and not the ‘initialization’

Thanks for reading this story.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rakesh Kumar Shaw
Rakesh Kumar Shaw

No responses yet

Write a response