JavaScript Hoisting and How to Use This

Hello dev's, today I'll show you what is JavaScript hoisting and how we can be benefitted from this. As by default Javascript hoisting is deprecated, we shoulod avoid using this. But it can be benefitted for us to take the idea or be sure to what happened on code. So, let's see how JavaScript Hoisting works.

JavaScript Hoisting

JavaScript hoisting means using a variable or function before its initialization. Let's see the below source code.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Javascript Hoisting - shouts.dev</title>
</head>
<body>
<script>  
x=10;  
console.log(x); 
var x;  

console.log(sum(10,20));  
function sum(a,b)  
{  
return a b;  
}  
</script>  
</body>
</html>

So, it'll run perfectly no error will be there and produce the below result. 

JavaScript Hoisting

 

It's good to know that Javascript Hoisting is JavaScript default behavior.

That's it for today. I hope you've enjoyed this tutorial. If you found any difficulties or questions, catch me in comment sections. Thanks for reading. ๐Ÿ™‚