0% found this document useful (0 votes)
2 views2 pages

Hoisting in JavaScript

Hoisting in JavaScript refers to the behavior where variable and function declarations are moved to the top of their scope before execution. Function declarations are fully hoisted, allowing them to be called before their definition, while variables declared with var are hoisted but initialized as undefined. Variables declared with let and const are also hoisted but remain in a 'temporal dead zone' until their declaration, resulting in an error if accessed beforehand.

Uploaded by

Prem Satarekar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Hoisting in JavaScript

Hoisting in JavaScript refers to the behavior where variable and function declarations are moved to the top of their scope before execution. Function declarations are fully hoisted, allowing them to be called before their definition, while variables declared with var are hoisted but initialized as undefined. Variables declared with let and const are also hoisted but remain in a 'temporal dead zone' until their declaration, resulting in an error if accessed beforehand.

Uploaded by

Prem Satarekar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hoisting in JavaScript

Step 1: Define Hoisting

Hoisting in JavaScript is the behavior where variable and function declarations are moved to the top

of their scope before code execution. This means JavaScript knows about these declarations even

before they are written in the code.

Step 2: Explain How It Works

With function declarations, the entire function is hoisted, so I can call the function anywhere in the

code, even before the function is written. On the other hand, when it comes to variables declared

with var, only the declaration is hoisted, not the value. So, the variable is initially undefined until it's

assigned a value later in the code.

Step 3: Provide an Example

For example, if I declare a function and call it before it is defined in the code, it works because

JavaScript hoists the function to the top:

```javascript

sayHello(); // Output: 'Hello'

function sayHello() {

[Link]('Hello');

```

However, with a variable declared using var, only the declaration is hoisted, so it will return

undefined if I try to access it before assignment:


```javascript

[Link](food); // Output: undefined

var food = 'Pizza';

[Link](food); // Output: 'Pizza'

```

In this case, var is hoisted, but its value is not assigned until the line where I set it.

Step 4: Mention let and const

Variables declared with let and const are also hoisted, but they remain in a 'temporal dead zone'

until the code reaches their declaration, so trying to use them before they are declared throws an

error.

Step 5: Wrap Up

To summarize, hoisting moves declarations to the top of the scope, allowing me to use functions

before they are declared, but variables declared with var are only hoisted with an undefined value

until they are assigned. let and const provide a stricter behavior and cannot be accessed before

they are initialized.

You might also like