- Before saying anything checkout the below example.var x = 8;function getName(){console.log('My name in Rahul More');}getName();console.log(x)
- What do you think is the output of the above code?
Yes, you guess that right. you will get output as you guessed it right. - But here is a trick. what if we do something like this?getName();console.log(x)var x = 8;function getName(){console.log('My name in Rahul More');}
- Now can you guess what the will be the output?
It's running Function getName but we got `undefined` for x - But if we remove `x = 8` from we will get an error that `x is not defined`.
Undefined Vs Not Defined
- Accessing variables & functions before declaring is known as Hoisting in JS.
- the function is working as it is even if we moved that at TOP but Variable is not working properly.
- Even before executing the code memory got allocated for variables, functions for other things.
0 comments:
Post a Comment