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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:14:09+00:00 2026-06-15T01:14:09+00:00

So lets say I have some code: //Javascript var elements = []; function addNumbah1(){

  • 0

So lets say I have some code:

//Javascript
var elements = [];
function addNumbah1(){
    var i = 1;
    elements.push(i);
}
function addNumbah2(){
    var i = 2;
    elements.push(i);
}

And that goes on up to addNumbah999(), is it bad form to declare the i variable every time? Will that break anything? Should I do:

//Javascript
var elements = [];
var i
function addNumbah1(){
    i = 1;
    elements.push(i);
}
function addNumbah2(){
    i = 2;
    elements.push(i);
}
  • 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-15T01:14:11+00:00Added an answer on June 15, 2026 at 1:14 am

    Short answer: NO, JS hoists all variable declarations to the top of the scope, regardless of how many times you’ve declared them:

    var i = 0
    for (var i=0;i<10;i++)
    {
        var j = i%2;//declared 10 times, on each iteration
    }
    

    Will be translated to

    var i, j; //i is undefined at this point in the code.
    for (i = 0;i<10;i++)
    {
        j = i%2;//declared 10 times, on each iteration
    }
    

    In your first example, you’re declaring i as a variable in a function’s scope, which is what you must do to avoid cluttering the global scope. The memory these variables use is allocated when the function is called, and deallocated when the function returns (roughly, closures form an exception, but that would take us to far). Consider this:

    var i = 10;
    function someF()
    {
        var i = 1;
        alert(i);
    }
    someF();//alerts 1 <-- value of i, local to someF
    alert(i);//10, global i is unchanged
    

    But if you were to omit the var:

    function someF()
    {
        i = 1;
        alert(i);
    }
    

    You’ll see that 1 is alerted twice. If JS can’t find a variable declaration in the current scope, it will look in the higher scopes until a var is found. If no variable is found, JS will create one for you in the highest scope (global). Check my answer here on how implied globals work for a more detailed example, or read the MDN pages, especially the section on Name conflicts

    Lastly, I’d like to add that globals, especially implied globals, are evil. Also know that the ECMA6 standard is clearly moving away from global variables and introduces support for true block-scopes. As you can see here
    Oh, and if you want to check if a function uses implied globals: 'use strict'; is a great thing:

    (function()
    {
        'use strict';
        var localVar = 123;//ok
        impliedGlobal = 123;//TypeError!
    }());
    

    As you can see, implied globals are not allowed. See MDN on strict mode for the full explanation

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

Sidebar

Related Questions

Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello
Lets say I have a bit of javascript code that is passed a string
Lets say i have this javascript code in an html pages <script type=text/javascript> $(document).ready(function(){
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
In my code, let's say I have the PaintObject(Graphics g) . In some other
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say I have multiple commands like 5000 which updates some column and row
Lets say I have one row with three columns - some buttons on left
Lets say I have a simple 1D array with 10-20 entries. Some will be
Lets say I have if (textbox.Text != null && textbox.Text.Length > someNum) { //some

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.