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 1077947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:39:05+00:00 2026-05-16T21:39:05+00:00

alert(myVar1); return false; var myVar1; Above code throws error in IE, FF and Opera

  • 0
alert(myVar1);
return false;
var myVar1;

Above code throws error in IE, FF and Opera stating that return statement has to come in the function. But it works (shows undefined) in Safari and Chrome.

The above code has been written in global scope. Outside all the functions.

Any reason?

  • 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-05-16T21:39:05+00:00Added an answer on May 16, 2026 at 9:39 pm

    In JavaScript variables are moved to the top of script and then run. So when you run it will do

    var myVar1;
    alert(myVar1);
    return false;
    

    This is because JavaScript doesn’t really have a true sense of lexical scoping. This is why it’s considered best practice to have all your variables declared at the top of the area they will be used to prevent hoisting causing a problem. JSLint will moan about this.

    This is a good article that explains it: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

    The return is invalid. If you want to do a true hoisting example (taken from the link above) do

    var foo = 1; 
    function bar() { 
        if (!foo) { 
            var foo = 10; 
        } 
        alert(foo); 
    } 
    bar();
    

    This will alert 10

    Below is my understanding and I have read it somewhere but can’t find all the sources that I read so am open to correction.

    This Alerts thanks to the differences in the JavaScript JIT. TraceMonkey(http://ejohn.org/blog/tracemonkey/) I believe will take the JavaScript and do a quick static analysis and then do JIT and then try to run it. If that fails then obviously nothing works.

    V8 doesn’t do the static analysis and moves to the JIT then runs so something. It’s more akin to Python. If you run the script in the Developer console (ctrl+shift+j in Windows) in Chrome it will throw an error but also run to give you the alert.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following is the code: var myVar = { test:function(){ alert('hello'); }, myinternalObj: { internalTest:
alert(/[a-zA-Z0-9 -_\.,]+/.test('test_string@'); I currently have the above code and I want to it to
alert($(this).children().next().html()); returns <a href=thelink.php?id=XXXX>click me...</a> How do I change above statement to grab only
NEWBIE ALERT! Here's the situation. I've got an Android ListActivity class (AppWindow) that contains
If the following works: alert( data.query.results.tbody[1].tr[0].td[0].span.img.src ); Why will this not work? var image
var myvar = my value; (function() { alert(myvar); // undefined })(); or var myvar
Spoiler alert: this is related to Problem 14 from Project Euler. The following code
Newbie alert! OK, I have a working central Mercurial repository that I've been working
NEWBIE ALERT! background: For the first time, I am writing a model that needs
The alert box will display nothing, and not return any data from any URL

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.