Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

JavaScript Hoisting

Hoisting is a concept in JavaScript, not a feature. In other scripting or server side languages, variables or functions must be declared before using it.

In JavaScript, variable and function names can be used before declaring it. The JavaScript compiler moves all the declarations of variables and functions at the top so that there will not be any error. This is called hoisting.

Example: Hoisting
x = 1;

alert('x = ' + x); // display x = 1

var x;
Try it

The following figure illustrates hoisting.

JavaScript Hoisting

Also, a variable can be assigned to another variable as shown below.

Example: Hoisting
x = 1;
y = x;

alert('x = ' + x);
alert('y = ' + y);

var x;
var y;
Try it

Hoisting is only possible with declaration but not the initialization. JavaScript will not move variables that are declared and initialized in a single line.

Example: Hoisting not applicable for initialized variables
alert('x = ' + x); // display x = undefined
        
var x = 1;
Try it

As you can see in the above example, value of x will be undefined because var x = 1 is not hoisted.

Hoisting of Function

JavaScript compiler moves the function definition at the top in the same way as variable declaration.

Example: Function Hoisting
alert(Sum(5, 5)); // 10

function Sum(val1, val2)
{
    return val1 + val2;
}
Try it

Please note that JavaScript compiler does not move function expression.

Example: Hoisting on function expression
Add(5, 5); // error

var Add = function Sum(val1, val2)
{
    return val1 + val2;
}
Try it

Hoisting Functions Before Variables

JavaScript compiler moves a function's definition before variable declaration. The following example proves it.

Example: Function Hoisting Before Variables
alert(UseMe);

var UseMe;

function UseMe()
{            
    alert("UseMe function called");
}
Try it

As per above example, it will display UseMe function definition. So the function moves before variables.

Points to Remember :
  1. JavaScript compiler moves variables and function declaration to the top and this is called hoisting.
  2. Only variable declarations move to the top, not the initialization.
  3. Functions definition moves first before variables.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.