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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:48:10+00:00 2026-05-14T01:48:10+00:00

function test(){ if(true){ var a = 5; } alert(a); } test(); I keep getting

  • 0
function test(){
    if(true){
        var a = 5;
    }
    alert(a);
}

test();

I keep getting ‘out of scope’ errors in my JS code when I check with JsLint which make no sense to me.So I quickly created an example. Is there something actually wrong with this code piece, as the variable is eventually hoisted to the top of the function anyways.

  • 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-14T01:48:10+00:00Added an answer on May 14, 2026 at 1:48 am

    While var localizes a variable to the function and is subject to hoisting, most languages have block scope and not function scope.

    By using the var keyword inside an if block, but accessing the variable outside that block, you’ve created a construct that can be confusing to people not familiar with that JS idiosyncrasy.

    Douglas Crockford recommends using a single var statement at the top of a function that specifies all the variables that should be scoped to that function.

    function test(){
        var a;
        if(true){
            a = 5;
        }
        alert(a);
    }
    
    test();
    

    With multiple variables you would have:

    function foo () {
        var a, b, c, d = "only d has an initial value", e;
        // …
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

test = function(x){ if ( some conditions ) { return true; } else {
I'm using the Node.js native driver. The following works fine db.collection(test).insert({hello:'world_safe'}, {safe: true}, function(err,
function test() { var formHTML =; var frmid =test123; var options = [Test1, Test2,
Consider a simple JS function as function test(property, color) { var element = document.getElementById(test);
I'm trying to create a function that will check to make sure all the
function test() { var flag1 = false; if ($('#field1').attr('value') || !$('#field1').attr('value')) { $.ajaxSetup({ cache:
I have JavaScript Function in which I check for value of a stageId box
I have the following script inside a HTML page: <script> function Test(){ alert(i got
So in default.aspx I have the code <script type=text/javascript> function validateForm() { test(); //here
I have some code for validating date below: function validateForm() { var errFound =

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.