I will post some questions in JavaScript from today onwards share your own solutions without running these code on your console.

  1. null === undefined

  2. null == undefined

  3. 2+"4"

  4. 2-"3"

  5. ""+2

  6. +"2"

Did you find this post useful? Show some love!

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:

  • false
false
null
0
undefined
""
NaN

I'm out. What are the last 2?

Unless I'm missing some trickery in the original comment, [] and {} are truthy values in JavaScript.

After some tests, I can confirm that they are true and false at the same time.

Javascript is weird.

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...

node -e "if (({})) console.log('truthy')" // requires wrapper to not interpret as block
node -e "if ([]) console.log('truthy')"

Logs truthy twice

[] == false // true
let obj = {}
obj == false // true

Oddly, node -e "if ([] == false) console.log('truthy')" logs truthy, but node -e "if ({} == false) console.log('truthy')" doesn't.

Oh Javascript...

The moral of the story: always use ===.

I used an extra variable for obj because {} == false got me a syntax error.

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:

$ node -p ';[false, null, undefined, 0, NaN, "", [], {}].map(v => [v?"X":"√︎",v])'
[ [ '√︎', false ],
  [ '√︎', null ],
  [ '√︎', undefined ],
  [ '√︎', 0 ],
  [ '√︎', NaN ],
  [ '√︎', '' ],
  [ 'X', [] ],
  [ 'X', {} ] ]

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):

  • -0
  • document.all

Without looking:

'1. null === undefined: ' + false;
'2. null == undefined: ' + true;
'3. 2+"4": ' + '24';
'4. 2-"3": ' + -1;
'5. ""+2: ' + '2';
'6. +"2": ' + 2;

I love these! I even have a codepen that's kind of like this

Right answers

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.

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

[deleted]

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.

[deleted]

Ah, yes.

I was talking about 3 and 5 :D

Oh i thought it was 5 and 6.
yeah 3 and 5 are Strings😁

Ben Halpern DEV.TO FOUNDER

Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

Plus, no fake comments when you're signed in. 🙃

  1. false [checking for type of data and value of data]
  2. true [because both are of the same type i.e. falsy]
  3. "24" [ coercion rules, concatenation takes place because of + operator]
  4. -1 [ -1, subtraction takes place as - operator is used on numbers not on strings]
  5. "2" [ again string concatenation}
  6. Not sure, tell us!

6 uses + as a short form for type conversion

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/1

null == undefined
// expected: false... but less confidently :/
// actual:   true
// score:    1/2

2+"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/3

2-"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

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

  1. false
  2. true
  3. "24"
  4. -1
  5. "2"
  6. true

Not sure about some of these.

Edit: Got five right!

Before running these statements:

false
true
24
-1
NaN
2

After running them

false
true
24
-1
"2"
2

4/6 😅

I guess :)

1. false
2. true
3. "24"
4. -1
5. "2"
6. 2
  1. false
  2. true(sure)
  3. "24." (not sure)
  4. "-1." (maybe)
  5. "2" (sure)
  6. ????
  1. false
  2. true
  3. "24"
  4. -1
  5. "2"
  6. 2

Looking forward for the next questions 😀

false
true
"24"
-1
"2"
error ?

Tested it, only have one wrong

Syntax error on 3 and 4, beware of trailing periods!

Edit: OP removed the trailing periods!

In the past I collected some strange behaviour of JavaScript:

github.com/bullgit/WAT/blob/master...

:-)

  1. false
  2. true
  3. "24"
  4. "2"
  5. "2"

??? Just a guess

Classic DEV Post from Jun 11

Who's looking for open source contributors? (June 11 edition)

Please shamelessly promote your project. Everyone who posted in previous weeks is welcome back this week, as always.

READ POST
Follow @ben to see more of their posts in your feed.
Sai gowtham
JavaScript developer, Writer & Aws certified solutions architect.
More from @saigowthamr
JavaScript Quiz Part 3
#webdev #javascript #challenge
JavaScript Quiz Part 2
#javascript #challenge #beginners
Trending on dev.to
I Want Scalar Objects in PHP
#php #webdev
The Top 3 Simple Security Threats for Web Dev
#security #webdev
8 Ultimate Full Stack Interview Questions and Answers
#fullstack #interview #node #javascript
Everyone is watching what you do online. How user tracking with cookies works
#security #privacy #webdev
Best Automated Testing Tools for 2018
#testing #selenium #devops #javascript
TIL props in React.js
#react #node #javascript #discuss
Guess.js – libraries and tools for enabling data-driven user-experience on the web
#webdev #javascript #react #angular
What happens when you type 'google.com' into a browser and press Enter?
#webdev