+ 1

Once a Boolean variable is declared is it automatically true?

I heard from someone that once you declare a Boolean variable and set it equal to a condition, that condition is automatically true, but, what happens if you don't set a condition?

10th Feb 2018, 1:26 AM
Jacob Barba
Jacob Barba - avatar
1 Resposta
0
In javascript not declarate variables are "undefined" and that is not the same as "false". Variable 0, -0, null, false, NaN or empty string is also "undefined" all other is true (false also). function check (){ var boolean1; var boolean2 = true; var boolean3 = false; if(boolean1){ console.log("boolean1 true"); } else{ console.log(boolean1); } if(boolean2){ console.log("bolean2 true"); } else{ console.log(boolean2); } if(boolean3){ console.log("bolean3 true"); } else{ console.log(boolean3); } } This code returns: undefined boolean2 true false
22nd Jun 2018, 2:38 PM
Dawid
Dawid - avatar