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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:24:40+00:00 2026-05-24T04:24:40+00:00

At w3schools there is written: If you declare a variable, without using var, the

  • 0

At w3schools there is written:

If you declare a variable, without using “var”, the variable always becomes GLOBAL.

Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler, but what is it good for? Better usage of RAM?

  • 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-24T04:24:41+00:00Added an answer on May 24, 2026 at 4:24 am

    No, there’s no RAM benefit or anything like that.

    What w3schools is talking about is something I call The Horror of Implicit Globals. Consider this function:

    function foo() {
        var variable1, variable2;
    
        variable1 = 5;
        varaible2 = 6;
        return variable1 + variable2;
    }
    

    Seems simple enough, but it returns NaN, not 11, because of the typo on the varaible2 = 6; line. And it creates a global variable with the typo’d name:

    function foo() {
        var variable1, variable2;
    
        variable1 = 5;
        varaible2 = 6;
        return variable1 + variable2;
    }
    console.log(foo());     // NaN
    console.log(varaible2); // 6?!?!?!

    This is because the function assigns to varaible2 (note the typo), but varaible2 isn’t declared anywhere. Through the mechanics of the scope chain in JavaScript, this ends up being an implicit assignment to a (new) property on the global object (which you can access as window on browsers, or globalThis in all modern environments [including modern browsers]).

    That’s just a "feature" of loose-mode JavaScript, assigning to a completely undeclared identifier isn’t an error; instead, it creates a property on the global object, and properties on the global object are global variables. (Up through ES5, all globals were properties of the global object. As of ES2015, though, a new kind of global was added that isn’t a property of the global object. Global-scope let, const, and class create the new kind of global.)

    My example is a typo, but of course, you could do it on purpose if you wanted. But I’d strongly advise not doing so. Instead, I recommend always using strict mode, either directly or by using ECMAScript modules (ESM, JavaScript’s own module system added in ES2015), which are strict by default. Strict mode makes assigning to an undeclared identifier an error rather than silently creating a global. If we’d been using strict mode, the problem with the foo function above would have been much more obvious and easier to diagnose:

    "use strict"; // Turns on strict mode for this compilation unit
    
    function foo() {
        var variable1, variable2;
    
        variable1 = 5;
        varaible2 = 6;                 // <=== ReferenceError
        return variable1 + variable2;
    }
    console.log(foo());

    Somewhat tangential, but in general I’d recommend avoiding globals wherever possible. The global namespace is already very, very cluttered on browsers, which makes it easy to accidentally create conflicts. The browser creates a global for every element in the DOM with an id, for most elements with a name, and has several predefined globals of its own (like name and title) which can easily conflict with your code.

    Instead, use JavaScript modules (ESM). Top-level declarations in a module aren’t globals, they’re only global to the code in that module. Then you can use export to intentionally expose the parts of your code you want other modules to be able to use (via import).

    Here in 2022, you can almost always use ESM; it’s well-supported by modern browsers and by Node.js. If you have to target an obsolete environment that doesn’t support it (like Internet Explorer), you can use a bundler to wrap up your ESM code into a bundle instead.

    If for some reason you can’t use ESM, you can do what we used to do before modules were standardized: Use a scoping function wrapped around your code:

    (function() {
        var your, symbols, here, if_they_need, to_be_shared, amongst_functions;
    
        function doSomething() {
        }
    
        function doSomethingElse() {
        }
    })();
    

    And if you do that, you might want to enable strict mode:

    (function() {
        "use strict";
        var your, symbols, here, if_they_need, to_be_shared, amongst_functions;
    
        function doSomething() {
        }
    
        function doSomethingElse() {
        }
    })();
    

    …which, as mentioned, has the advantage of turning assignments to undeclared identifiers into errors (along with various other helpful things).

    If you have to make something global, you can assign to a property on window. (In modern environments, I’d say you can assign to a property on globalThis, but if you can’t use ESM, it’s unlikely the environment you’re targeting supports globalThis.)

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

Sidebar

Related Questions

I think this property is quite useful, http://www.w3schools.com/Dom/prop_document_xml.asp But as you can see, it's
What is the difference between these two: font-style:italic font-style:oblique I tried using the W3Schools
ajax tutorial on w3school at http://www.w3schools.com/ajax/ajax_database.asp In this function (function GetXmlHttpObject()), it creates a
I'm using the W3 Schools example: http://www.w3schools.com/PHP/php_ajax_database.asp to display a table of database data
w3schools says that exceptions can be strings, integers, booleans, or objects, but the example
there is a built-in function ( sleep() ) in php that delays execution of
I've seen that history.go() method can have two types of parameter: see: http://www.w3schools.com/jsref/met_his_go.asp But
There is a jQuery quiz posted on the W3Schools site here... http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery Question #16
I was searching at w3schools about the meta tag , but there's nothing about
Hi guys I'm using this code http://www.w3schools.com/php/php_file_upload.asp to upload files and it works great

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.