DEV Community

Sai gowtham
Sai gowtham

Posted on

JavaScript Quiz

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"

Latest comments (40)

Collapse
 
crazylxr profile image
lxr

false
true
string:24
number:-1
string:2
number:2

Collapse
 
fouzelddin profile image
Fouzeddin

false
true
6
-1
"2"
2

Collapse
 
barthclem profile image
Oyeyemi Clement
  1. False
  2. True
  3. "24"
  4. 1
  5. 2
  6. "2"

Well, let me run to my console to confirm that I know this nice language with weird behavior

Collapse
 
sait profile image
Sai gowtham

sure....

Collapse
 
learosema profile image
Lea Rosema (she/her)

I guess :)

1. false
2. true
3. "24"
4. -1
5. "2"
6. 2
 
joshcheek profile image
Josh Cheek

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', {} ] ]
Collapse
 
joshcheek profile image
Josh Cheek

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
 
caseywebb profile image
Casey Webb • Edited

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

Collapse
 
val_baca profile image
Valentin Baca

Got 3 and 6 wrong. Neat quiz!

 
caseywebb profile image
Casey Webb • Edited

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

Collapse
 
luigi_leano profile image
Luis Uriel Leaño
  1. false
  2. true
  3. 24
  4. NaN
  5. 2
  6. 2
 
fargrim profile image
Kyle Blake

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

Collapse
 
jjjjcccjjf profile image
endan
  1. false
  2. true
  3. "24"
  4. "2"
  5. "2"

??? Just a guess

Collapse
 
johndemian profile image
John Demian

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.

Collapse
 
brunojennrich profile image
bruno jennrich

0.30000000000000004

but this would happen on any IEEE floating point machine

Collapse
 
isabolic99 profile image
isabolic99
  1. false
  2. true
  3. "42"
  4. -1
  5. "2"
  6. 2
Collapse
 
isabolic99 profile image
isabolic99

got 6 wrong

Collapse
 
andreandyp profile image
André Michel Andy

Before running these statements:

false
true
24
-1
NaN
2

After running them

false
true
24
-1
"2"
2

4/6 😅