Full-time web dev; JS lover since 2002; CSS fanatic. #CSSIsAwesome
I try to stay up with new web platform features. Web feature you don't understand? Tell me! I'll write an article!
He/him
Here they all are. I realize that two of them will bring out the angry mob with their torches and pitchforks. But my disclaimer is that I did not write the language.
First the easy 6 falsy things:
false
0
""
undefined
null
NaN
Plus the final two falsy things, the second of which being an HTML-ism (part of the HTML specification):
I am a software developer currently working as a consultant. I work primarily in Javascript, React, and RxJS. I am always trying to learn new languages and frameworks.
Both convert the number to string and do a string concatination, even in one the number is the first operand and in the other the string is the first operand.
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 like false would 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"+4 right after 2+"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, so 2+"4" would be calling Integer#+ and "2"+4 would be calling String#+, 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.
null===undefined// expected: false// actual: false// score: 1/1null==undefined// expected: false... but less confidently :/// actual: true// score: 1/22+"4"// expected: Jesus >.< uhm... 6? If not 6, than 24, pretty sure// one of them gets cast, just not sure which.// actual: fuuuuuuck, 24 >.<// score: 1/32-"3"// expected: Based on the above, I'm really hoping it's -1// actual: -1// score: 2/4""+2// expected: Oh fuck. uhhhh. maybe it comes in as zero, or maybe// it gets treated like a character string instead of// a number string, which maybe casts the 2 to a string// ...wait, yeah, it's definitely "2"!// actual: "2"// score: 3/5+"2"// expected: Probably 2, right? I'd expect it to cast the "2" to// a number and then make it "positive" or something.// actual: 2// score: 4/6
One of my favorite web-slinger questions (for fun, not for job interviews) is: can you name all 8 of the falsy things?
Here's the first falsy thing, to get you started, which is pretty much a gimme:
I'm out. What are the last 2?
Shoutout! I didn't get
NaN-NaN and -0
Here they all are. I realize that two of them will bring out the angry mob with their torches and pitchforks. But my disclaimer is that I did not write the language.
First the easy 6 falsy things:
Plus the final two falsy things, the second of which being an HTML-ism (part of the HTML specification):
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
truthytwiceWithout looking:
I love these! I even have a codepen that's kind of like this
Oh, yeah, I know my weird Javascript math 🎉
1 and 2 I know because of work since null and undefined can be a pain when testing.
3 is string concatination with 2 being made a string to make it magically work.
4 is making the string a number to make the math operator magically work.
5 is similar to 3 in that it's concatinating an (empty) string with a number that magically becomes a string.
6 is fun because
+is the shorthand for type converting to an int.Unless I'm missing some trickery in the original comment,
[]and{}are truthy values in JavaScript.Got 3. and 5. wrong, haha.
I supposed that the first operand would decide which implementation of + would be used, but I guess the rule is: string wins :D
Both convert the number to string and do a string concatination, even in one the number is the first operand and in the other the string is the first operand.
Ah, yes.
I was talking about 3 and 5 :D
Oh i thought it was 5 and 6.
yeah 3 and 5 are Strings😁
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:6 uses + as a short form for type conversion
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
===.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.falsetrue"24"-1"2"trueNot sure about some of these.
Edit: Got five right!
Before running these statements:
After running them
4/6 😅
Uhhh I love these!
I've got one for you!
Without running this in the console let me know how much is
0.1 + 0.2
Hint: it's a number bigger than the one you are thinking of right now.
0.30000000000000004
but this would happen on any IEEE floating point machine
Looking forward for the next questions 😀
I guess :)
got 6 wrong
false
true
"24"
-1
"2"
error ?
Tested it, only have one wrong
good try
Well, let me run to my console to confirm that I know this nice language with weird behavior
sure....
Syntax error on 3 and 4, beware of trailing periods!
Edit: OP removed the trailing periods!
false
true
string:24
number:-1
string:2
number:2
In the past I collected some strange behaviour of JavaScript:
github.com/bullgit/WAT/blob/master...
:-)
??? Just a guess
Got 3 and 6 wrong. Neat quiz!
false
true
6
-1
"2"
2