+ 3
Why is this 1?
var x; if { x=1; alert(â1â) } else { alert(â2â) } The variable is undefined, how can it equal 1?
15 Answers
+ 5
= <---- That operator assigns a value to the variable.
==
=== >--- Those operators are comparing the values.
If(x=1) <-- That assigns 1 to x and then executes the code block. .
if(x==1) <--- That checks if x is equal to 1, and if it is then it'll run the code block,, if not it'll run the ELSE code block.
IF code block is in the same scope context as its parent (where it's located). Javascript is really odd with its scope sometimes and automatically raises a lot of stuff, so I would take some time to read through JS scoping also.
+ 4
brains and Jacob are correct. since if expects a Boolean value in return and the result of x equals 1 is non false result is true
+ 3
var x=5
if(x=6) still evaluates to true due to the operators nature
+ 3
variables are scoped by block. some compilers will throw an error if you declare a variable that has already been declared. in cases where the compiler lets you it declares a new variable within the scope of the current block. since the if statement is a new block most compilers will allow this and it'll be treated as another variable separate from the one declared outside the if block
note: it is considered bad practice
+ 2
Because you didn't give your IF a condition, so nothing equals nothing, which is true, thus your IF code block is what executed instead of your ELSE code block.
+ 2
Thank you, Jakob. This has to do with scope then, right? Since the outside variable was undefined the inside variable reigned supreme so to speak. Appreciate your time.
+ 2
Honestly though. I'm surprised it didn't give you a compile error because it's not correct syntax. My opinion is based solely upon what you posted and the fact that you said you got an output of 1.
Also, if it's a typo, and your actual code is this:
if (x=1) {
alert(â1â)
}
else {
alert(â2â)
}
^In that scenario, the problem is that '=' is an assignment operator and NOT a comparison operator (== or ===). So it's assigning the value of 1 to x, which if it's successful then that means the condition is true, so the IF code block executes.
+ 2
@Levi
You're welcome. Can you post your code into Code Playground for me? If I can see it within context and its output, I can tell you 100% what's going on.
+ 2
note
if(x=1) is not
if(x==1)
.in situation 1 you are assigned a value to x not making a boolean comparision
+ 2
nope it wouldnt
+ 2
If var x is declared outside the if statement and then the if statement declares it a different value will that different value only apply to the if statement or will it change the variable outside as well? Thank you both for your kind replies. You are really helping me to grasp this key concept.
+ 2
Thank you as well Michael
+ 2
That is what I thought. I knew that applied to functions but wasnât sure if it applied to all blocks. Really helpful. Thank you.
+ 1
It was a JS challenge question. You were correct... I had screwed up the syntax when copying here. It is as you posted. Thank you. I understand. If x was assigned a value, however, the if statement would not reassign it, correct? Wouldnât that variable remain within the scope of the if statement? Or does that only apply to functions? Thanks for your help.
+ 1
I see Brains... but would that change if var x had initially been given a value? I mean, I understand the operators but trying to wrap my head around the scope.