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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:21:08+00:00 2026-06-02T06:21:08+00:00

I want to know what’s the difference when declare a variable whether using var

  • 0

I want to know what’s the difference when declare a variable whether using var. i used the following code:

<body>
    <h1>New Web Project Page</h1>
    <script type="text/javascript">
        function test(){
            a = "hello";
            var b="world";
        }
        alert(a);
            alert(b);


    </script>
</body>

Why the alert doesn’t work and what’s the difference when declare a variable whether using var in javascript.

  • 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-02T06:21:09+00:00Added an answer on June 2, 2026 at 6:21 am

    alert doesn’t work because, in the case of b, it doesn’t exist in the correct scope and in the case of a it hasn’t been instantiated yet because you haven’t called the function test

    var creates a variable which is local to the scope in which it is called. Without var you create a global variable.

    function fn ( ) {
        var x = 0; // this variable is local to the function fn
    
        y = 10; // this variable is global and can be accessed
                // from anywhere after you call fn
    }
    

    generally speaking, unless there is a good reason you don’t want to use global variables. That is to say, you want to create all of your variables using var. Putting variables only in the scope they are needed makes for easier to understand and maintain code. It also helps to alleviate the “Magic Variable” problem. Where you have variables that just appear but you don’t have a clear idea of from where. It also makes it easier to debug your code because, in javascript, variables that don’t exist get created on the fly. But if you only use local variables through var and a tool like jsLint you won’t run into the problem of a misspelling in a variable name throwing your code out of wack.

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

Sidebar

Related Questions

Hi friends i want know sectionIndexTitlesForTableView Action is works i have the following code...
I want to know what is the main technical difference between Web (ASP, PHP,
I have created one web application using PHP. I want know the IP randomly
want to know why String behaves like value type while using ==. String s1
Want to know what is the difference between ipv4 and ipv6 and how does
I want to know the difference between : $this->forward(module, action); And $this->redirect(module/action); My first
I'm a bit new to the C#-form app developing and I want know, what's
I've found an emulator for 80x86 created by using Java, and I want know,
I want know how I can add values to my vector of structs using
hello i am new in android i want know how to iterate child node

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.