Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will I have any problems if I declare the same variable multiple times?

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);
}
like image 548
ChapmIndustries Avatar asked Nov 29 '12 16:11

ChapmIndustries


People also ask

Can we declare variables multiple times?

We cannot declare the same variable multiple times if one of them is declared using let, whereas we can declare the same variable any number of times using var. Variable hoisting can be done using var, but hoisting cannot be done using let.

How many times can you declare a variable?

a variable/function can be declared any number of times but it can be defined only once.

Do you have to declare a variable every time you use it?

All variables must be declared before they can be used.

Can you declare a variable twice in C?

int a; means both declaration and definition, but we know that defining a variable more than once is not allowed.


1 Answers

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

like image 127
Elias Van Ootegem Avatar answered Oct 02 '22 09:10

Elias Van Ootegem