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

Share edited Feb 12, 2017 at 11:28 luiscrjr 7,2783 gold badges27 silver badges28 bronze badges asked Feb 12, 2017 at 11:24 eggsyeggsy 411 silver badge3 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Actually, 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