admin管理员组

文章数量:1287789

I'm really bad a javascript please don't get me wrong if you find this question dumb; if I have two variables, should I have declared them inside or outside the function?

like this

var Num = document.querySelector("#Fnumber");
var Num2 = document.querySelector("#Secnumber");
function Multiply(){
alert(Num.value + Num2.value)
}

//or declaring variable inside a function like this ?

function Multiply(){
var Num = document.querySelector("#Fnumber").value;
var Num2 = document.querySelector("#Secnumber").value;
alert(Num + Num2)
}

and am I pasting the value Property correct on both examples ?

I'm really bad a javascript please don't get me wrong if you find this question dumb; if I have two variables, should I have declared them inside or outside the function?

like this

var Num = document.querySelector("#Fnumber");
var Num2 = document.querySelector("#Secnumber");
function Multiply(){
alert(Num.value + Num2.value)
}

//or declaring variable inside a function like this ?

function Multiply(){
var Num = document.querySelector("#Fnumber").value;
var Num2 = document.querySelector("#Secnumber").value;
alert(Num + Num2)
}

and am I pasting the value Property correct on both examples ?

Share Improve this question edited May 5, 2019 at 15:34 shivlal kumavat 8881 gold badge13 silver badges29 bronze badges asked May 5, 2019 at 14:02 GloryhackerGloryhacker 431 silver badge5 bronze badges 1
  • 1 Depends on how you have everything else set up. Encapsulation is good for reuse. The issue is that each time it is called (if in a function) it would need to redetermine what NUM and NUM2 are which takes N time. But usually i define the inputs as private properties of a class. – Fallenreaper Commented May 5, 2019 at 14:11
Add a ment  | 

3 Answers 3

Reset to default 6

Both are valid, you just have to be aware of the different scope the variables will be in. In the first example, the variables will be available outside of the function Multiply, they could be modified by other functions. In the second example, they are only available inside the Multiply function. When choosing where to declare your variables, you should ask yourself where you need to be able to use them, whether you are polluting the global scope, and how much of a performance hit are you taking when you declare them.

I think declaring them outside is better because when you declare them inside you are selecting the inputs every time you run the function Multiply(). In the case of two inputs it will not effect the performance but in case of many elements it will definitely affect the performance.

But when you declare the elements outside you need to just access their value.

This also depends upon the importance of the elements Num and Num2. If the whole app uses them alot and they are used in other functions too then the variables should be declared outside

This is good if you are using only in one function this variable value.

function Multiply(){
var Num = document.querySelector("#Fnumber").value;
var Num2 = document.querySelector("#Secnumber").value;
alert(Num + Num2)
}

本文标签: javascriptShould I declare a variable inside a function or outsideStack Overflow