I am wondering if it is possible to access the value of the condition directly, as in the following example.
var a = ["pear", "kiwi", "orange", "apple"] if(a.indexOf("orange") !== -1){ console.log(this) //as a.indexOf("orange") has been evaluated already above this prints 2 }
It will also make triple operators less bloated.
var a = ["pear", "kiwi", "orange", "apple"] var b = ((a.indexOf("orange") !== -1) ? this : '')
thanks
EDIT: Eliminating this issue for future visitors. Basically this question is about getting the obtained value of what is evaluated in the if / else statement. In the example
var a = ["pear", "kiwi", "orange", "apple"] if(a.indexOf("orange") !== -1){ //is basically if(2 !== -1) console.log(this) //would then be "2" from the already evaluted a.indexOf from above }
source share