Why is this 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why is this 1?

var x; if { x=1; alert(“1”) } else { alert(“2”) } The variable is undefined, how can it equal 1?

4th Apr 2018, 7:32 PM
Levi
Levi - avatar
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.
4th Apr 2018, 7:58 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 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
4th Apr 2018, 8:12 PM
Michael Simnitt
Michael Simnitt - avatar
+ 3
var x=5 if(x=6) still evaluates to true due to the operators nature
4th Apr 2018, 7:54 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 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
4th Apr 2018, 8:27 PM
Michael Simnitt
Michael Simnitt - avatar
+ 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.
4th Apr 2018, 7:36 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 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.
4th Apr 2018, 7:39 PM
Levi
Levi - avatar
+ 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.
4th Apr 2018, 7:39 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 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.
4th Apr 2018, 7:41 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 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
4th Apr 2018, 7:46 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 2
nope it wouldnt
4th Apr 2018, 7:53 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 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.
4th Apr 2018, 8:24 PM
Levi
Levi - avatar
+ 2
Thank you as well Michael
4th Apr 2018, 8:25 PM
Levi
Levi - avatar
+ 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.
4th Apr 2018, 8:28 PM
Levi
Levi - avatar
+ 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.
4th Apr 2018, 7:47 PM
Levi
Levi - avatar
+ 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.
4th Apr 2018, 7:51 PM
Levi
Levi - avatar