Shouldnt console.log output true instead of numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

16th Mar 2019, 8:59 AM
Drinald Varfi
Drinald Varfi - avatar
4 Answers
+ 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 ==.
16th Mar 2019, 9:15 AM
Fermi
Fermi - avatar
+ 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
16th Mar 2019, 9:25 AM
Drinald Varfi
Drinald Varfi - avatar
+ 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
16th Mar 2019, 4:20 PM
George S Mulbah II
George S Mulbah II - avatar
0
George S Mulbah console.log(1===1) returns 1 aswell
16th Mar 2019, 4:40 PM
Drinald Varfi
Drinald Varfi - avatar