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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:21:13+00:00 2026-05-25T00:21:13+00:00

In reference to setting global and local variables, I’ve read about memory leaks and

  • 0

In reference to setting global and local variables, I’ve read about memory leaks and how to avoid them. I am still a little confused.

Let’s just say that these examples occur deep within pages, where there are other global and local variables being used.

If I don’t plan to reuse a variable, should I or should I not use var?

If I use var once within a function, do I need to use it again when I manipulate or modify the variable?

// EMAMPLE ONE
$("#AddBrandSave").click(function() {
    // DO THIS?
    Brand = $("#Brand").val();
    // OR DO THIS?
    var Brand = $("#Brand").val();
});

// EMAMPLE TWO
$("#AddBrandSave").click(function() {
    // DO THIS?
    Brand = $("#Brand").val();
    Brand = Brand.substring(7); 
    Brand = Brand.someMathFunction(); 
    // OR DO THIS?
    var Brand = $("#Brand").val();
    Brand = Brand.substring(7); 
    Brand = Brand.someMathFunction; 
    // OR DO THIS?
    var Brand = $("#Brand").val();
    var Brand = Brand.substring(7); 
    var Brand = Brand.someMathFunction(); 
});
  • 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-25T00:21:13+00:00Added an answer on May 25, 2026 at 12:21 am

    Understanding var

    Javascript has exactly two levels of scope, global and function. When you use the var, the declaration of that variable is hoisted to the top of the scope it is in. For instance:

    $("#AddBrandSave").click(function() {
        var Brand = $("#Brand").val();
        var Brand = Brand.substring(7); 
        var Brand = Brand.someMathFunction(); 
    });
    

    is interpreted as

    $("#AddBrandSave").click(function() {
        var Brand;    
        Brand = $("#Brand").val();
        Brand = Brand.substring(7); 
        Brand = Brand.someMathFunction(); 
    });
    

    If you had not included var inside a function, “Brand” will be declared in the global scope, so

    $("#AddBrandSave").click(function() {
        Brand = $("#Brand").val();
        Brand = Brand.substring(7); 
        Brand = Brand.someMathFunction(); 
    });
    

    is interpreted as:

    var Brand; 
    $("#AddBrandSave").click(function() { 
        Brand = $("#Brand").val();
        Brand = Brand.substring(7); 
        Brand = Brand.someMathFunction(); 
    });
    // The global variable Brand will only exist after #AddBrandSave is clicked
    // or it will clobber the global variable Brand if it already existed
    

    This is why it is suggested that you declare any variables you intend to use in a function with var at the top of that function, because that is what technically happens anyway so it makes it easier to understand.

    Using var multiple times for the same variable could cause issues because it makes the interpreter work a little harder to check if that variable already exists – I don’t know if it causes memory leaks, but why risk it?

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

Sidebar

Related Questions

With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following:
In a webcrawler/webscraper-setting, I'd like to dynamically extend my base Reference Class URL in
How do I get the reference to a folder for storing per-user-per-application settings when
With reference to Problem with date day/month reversing on save I have further noted
I reference another eclipse project from the current project I am working on. Everything
For reference, the code is for the motorola 68008. Say I have code such
In reference to this answer to a Stack Overflow question : what is bench-testing
I reference the current page in a class with Page page = HttpContext.Current.CurrentHandler as
Object reference not set to an instance of an object. using System; using System.Collections.Generic;
For reference, I'm using Visual Studio 2010. I have a custom build step defined

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.