I will post some questions in JavaScript from today onwards share your own solutions without running these code on your console.
null === undefined
null == undefined
2+"4"
2-"3"
""+2
+"2"
I will post some questions in JavaScript from today onwards share your own solutions without running these code on your console.
null === undefined
null == undefined
2+"4"
2-"3"
""+2
+"2"
For further actions, you may consider blocking this person and/or reporting abuse
Latest comments (40)
false
true
string:24
number:-1
string:2
number:2
false
true
6
-1
"2"
2
Well, let me run to my console to confirm that I know this nice language with weird behavior
sure....
I guess :)
You have to wrap it in parens b/c JS treats toplevel curlies as a "block" rather than an object. But, of course, if you begin a line with parens, then it will become a function call on the previous line, if one exists, so for sanity's sake, begin any paren line with a semicolon (same for brackets).
Personally, I think it's really iffy to say that
{}and[]are falsy values. I've always understood "falsy" to mean you can drop it into a conditional and it will behave likefalsewould have:I scored 4 out of 6. But if I wasn't able to run them as I went, it would be much lower.
Also, I feel like you should add
"2"+4right after2+"4", b/c I was definitely wondering if the casting decision was based on which one was first. Eg in Ruby,+is a method call, so2+"4"would be callingInteger#+and"2"+4would be callingString#+, thus the ordering implies different behaviour, even though+in math is commutative. I know that's not how JS works, but it means that I would at least be entertaining the possibility of non-commutativity.Oddly,
node -e "if ([] == false) console.log('truthy')"logstruthy, butnode -e "if ({} == false) console.log('truthy')"doesn't.Oh Javascript...
The moral of the story: always use
===.Got 3 and 6 wrong. Neat quiz!
I can't think of any case where those would be falsy. What exactly did you experiment with that made it appear that way?
Sanity check...
Logs
truthytwice