Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9322823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:11:28+00:00 2026-06-19T04:11:28+00:00

In this mozilla article, I read: Another unusual thing about variables in JavaScript is

  • 0

In this mozilla article, I read:

Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense “hoisted” or lifted to the top of the function or statement. However, variables that aren’t initialized yet will return a value of undefined.

And then some examples:

/**
 * Example 1
 */
console.log(x === undefined); // logs "true"
var x = 3;


/**
 * Example 2
 */
// will return a value of undefined
var myvar = "my value";

(function() {
  console.log(myvar); // undefined
  var myvar = "local value";
})();

Example 2, above, will be interpreted the same as:

var myvar = "my value";

(function() {
  var myvar;
  console.log(myvar); // undefined
  myvar = "local value";
})();

I fail to see anything being “hoisted”—at least not in the sense that I traditionally interpret the definition of the word: it seems like variables are undefined until after they are declared. In what sense can you “refer to a variable declared later”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:11:29+00:00Added an answer on June 19, 2026 at 4:11 am

    When you use var it is “hoisted” to the top of the function declaration. Let’s look at the second example again:

    var myvar = "my value";
    
    (function() {
      console.log(myvar); // undefined
      var myvar = "local value";
    })();
    

    Notice how var myvar = 'my value' is declared first. Next, within the function scope, console.log(myvar) is called. The result is “undefined.” Why? You’d think it would be “my value” because that’s the order that the code is in.

    Because the local variable var myvar in the function scope is hoisted, it’s not defined. This is essentially equivalent to writing the function like this:

    (function() {
      var myvar;
      console.log(myvar); // undefined
      myvar = "local value";
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read the following Mozilla Developer article that explains how to implement HTML 5
So I found this awesome article about fading between images using javascript (using opacity,
i just read this article : https://developer.mozilla.org/en/HTTP_Caching_FAQ There's a firefox behavior (and some other
I've read this article on MDN: https://developer.mozilla.org/en/Places_utilities_for_JavaScript#Bookmark_Dialog But still has no clue how to
in this mozilla article, the following phase is used Parameters variable Variable to iterate
[EDIT: this question is about Mozilla Audio Data API which is no longer considered
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty states: configurable : True if and only if the type of this property
According to this Mozilla article on Ogg media , media works more seamlessly in
Where does the variable options.eventTarget come from? How can I fix this function for
I stumbled across this article which was talking about AJAX and jQuery and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.