+ 3
Shouldnt console.log output true instead of numbers?
Why does console.log(1==1) or console.log(2>1) outputs 1? Shouldn’t it output a true? When used the same on alert(1==1) outputs true. The console keeps outputting 1 for true and 0 for false is there any reason behind it?
4 odpowiedzi
+ 6
I think I've found why.
https://dev.to/mehraas/avoid-boolean-values-comparison-with--in-javascript-1lok
"In JavaScript, "==" compare number values instead of boolean values by performing implicit coercion."
alert() probably does type conversion back to boolean whenever necessary, while console.log returns the raw values resulting from the evaluation of ==.
+ 1
Fermi i see makes sense but its quite surprising to compare booleans or strings and to get a number as output 🤣. tnx for the explanation
+ 1
For me I just hate to use == so to avoid problem I use === , one behavior is that the == wasn't build to compare boolean value that return true or false it was build to compare values
0
George S Mulbah console.log(1===1) returns 1 aswell