Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank? [duplicate]

As part of my learning JavaScript, I try to write code to demonstrate the concept I am learning; today I'm learning hoisted variables. Here is the code I wrote:

console.log("A: My name is " + name);   

function happy() {
  console.log ("1: I am " + feeling);   
    var feeling = "happy";
  console.log ("2: I am " + feeling);   
}
happy(); 

var name = "Jim";
console.log("B: My name is " + name);   

I expected the following results:

A: My name is undefined
1: I am undefined
2: I am happy
B: My name is Jim

However, when testing my code at WriteCodeOnline.com and in another sandbox, the first console.log displays A: My name is. I am using a Chrome browser, if that makes a difference.

So, my question is, why does the hoisted local variable within the function return undefined while the hoisted global variable returns a blank?

like image 418
JimLockwood Avatar asked Dec 30 '14 22:12

JimLockwood


People also ask

Why is my global variable undefined JavaScript?

The reason the first alert is undefined is because you re-declared global as a local variable below it in the function. And in javascript that means from the top of the function it is considered the local variable.

Are global variables hoisted in JavaScript?

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Inevitably, this means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.

Are global variables hoisted?

Variable HoistingIf the variables are declared in the global scope then all the variables are lifted and declared at the top of the global context by Javascript. Similarly, in functions, all the variable declarations in the functions are lifted and declared at the top of the function.

How do you define a global variable in JavaScript?

Global Variables are the variables that can be accessed from anywhere in the program. These are the variables that are declared in the main body of the source code and outside all the functions. These variables are available to every function to access. Var keyword is used to declare variables globally.


1 Answers

What is happening here is that you are accessing window.name.

This is a predefined property on window, so your hoisted var name isn't actually creating a new variable. There's already one in the global scope with that name and by default, it has a blank string value.

To observe the behavior you were expecting, you can use a variable name other than name, or put your code inside a function:

function hoisting() {
  console.log("A: My name is " + name);   

  function happy() {
    console.log ("1: I am " + feeling);   
    var feeling = "happy";
    console.log ("2: I am " + feeling);   
  }
  happy(); 

  var name = "Jim";
  console.log("B: My name is " + name);   
}

hoisting();
like image 175
JLRishe Avatar answered Sep 20 '22 16:09

JLRishe