admin管理员组文章数量:1336189
1st if condition:
if (currentQuestionIndex == 2) {
var xvalue = currentscore;
}
2nd if condition: (note: current score increments for every currentQuestionIndex
)
if (currentQuestionIndex == 5 && currentscore ==xvalue) {
console.log('thanks for your participation')
}
I want to access the value of xvalue
in 1st if condition and reference it in 2nd if condition. Is there any way to store value of a variable and reference it later without regards to if condition
1st if condition:
if (currentQuestionIndex == 2) {
var xvalue = currentscore;
}
2nd if condition: (note: current score increments for every currentQuestionIndex
)
if (currentQuestionIndex == 5 && currentscore ==xvalue) {
console.log('thanks for your participation')
}
I want to access the value of xvalue
in 1st if condition and reference it in 2nd if condition. Is there any way to store value of a variable and reference it later without regards to if condition
4 Answers
Reset to default 3Actually, you can access a variable declared with var
inside if
statement. A if
block (or any block) does not create a new scope: in JavaScript, only a function do that.
This is valid (example from the docs):
if (true) {
var x = 5;
}
console.log(x); // x is 5
This happens because JavaScript interpreter scans your function before executing it, and keep track of all variable declarations (like moving it to the top).
So, the above code is equivalent to (interpreter perspective):
var x;
if (true) {
x = 5;
}
console.log(x); // x is 5
Quoting from MDN:
JavaScript before ECMAScript 2015 does not have block statement scope; rather, a variable declared within a block is local to the function (or global scope) that the block resides within.
As pointed, this behavior changes with the introduction of ECMAScript 2015 let
.
if (true) {
let x = 5;
}
console.log(x); //x is not defined
So, to use your code as an example, this will run fine:
/* arbitrary values */
var currentQuestionIndex = 2;
var currentscore = 10;
if (currentQuestionIndex == 2) {
var xvalue = currentscore; //declared inside if block
}
if (currentQuestionIndex == 2 && xvalue == 10) { //I can read here
console.log('value of xvalue: ' + xvalue); //can read here too: will print 10
console.log('thanks for your participation')
}
Anyway, just because you can, doesn't mean you should. As a matter of readability and code organization, a remended practice is to declare your variable at the beginning of your scope (global, if outside any function, or local, if inside a function).
Just define the variable xvalue
outside of if
statement:
///Just for testing
var currentQuestionIndex = 2
var currentscore = 2
///
var xvalue = 0;
if (currentQuestionIndex == 2) {
xvalue = currentscore;
}
///Just for testing
currentQuestionIndex = 5
currentscore = 4
console.log('xvalue: ' + xvalue)
///
if (currentQuestionIndex == 5 && currentscore == xvalue) {
console.log('thanks for your participation')
///Just for testing
console.log('xvalue: ' + xvalue)
///
}
And read about variable scopes, for example here.
You can set your xvalue
variable outside of your if statement.
var xvalue = 0;
or simply:
var xvalue;
This way the variable is always able to be accessed by any other function, even if you haven't given it a value.
Just declare xvalue
outside the first if
like this:
var xvalue; // decalare it here so it can be accessed by both if statements (probably you'll have to initialize it with something)
if (currentQuestionIndex == 2) {
xvalue = currentscore; // don't redeclare it here so you won't shadow the above one
}
Read more about scopes, variables lifetime and shadowing!
本文标签: javascriptstore variable value inside a if statementStack Overflow
版权声明:本文标题:javascript - store variable value inside a if statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742400870a2467856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论