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"

Oldest comments (40)

Collapse
 
kayis profile image
K

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

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
kayis profile image
K

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.

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
kayis profile image
K

Ah, yes.

I was talking about 3 and 5 :D

Thread Thread
 
sait profile image
Sai gowtham

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

Collapse
 
rezwanhossain profile image
Rezwan Hossain
  1. false
  2. true(sure)
  3. "24." (not sure)
  4. "-1." (maybe)
  5. "2" (sure)
  6. ????
Collapse
 
sudiukil profile image
Quentin Sonrel • Edited
  1. false
  2. true
  3. "24"
  4. -1
  5. "2"
  6. 2

Looking forward for the next questions 😀

Collapse
 
shreyasminocha profile image
Shreyas Minocha • Edited
  1. false
  2. true
  3. "24"
  4. -1
  5. "2"
  6. true

Not sure about some of these.

Edit: Got five right!

Collapse
 
antoine_m profile image
Antoine Mesnil

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

Tested it, only have one wrong

Collapse
 
sait profile image
Sai gowtham

good try

Collapse
 
antogarand profile image
Antony Garand • Edited

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

Edit: OP removed the trailing periods!

Collapse
 
kauresss profile image
Kauress
  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!
Collapse
 
seye profile image
˗ˏˋ Seye Kuyinuˊˎ˗‏

6 uses + as a short form for type conversion

Collapse
 
itsasine profile image
ItsASine (Kayla)

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

Collapse
 
itsasine profile image
ItsASine (Kayla)

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.

Collapse
 
0x04 profile image
Oliver Kühn • Edited

In the past I collected some strange behaviour of JavaScript:

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

:-)

Collapse
 
eljayadobe profile image
Eljay-Adobe

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
Collapse
 
caseywebb profile image
Casey Webb • Edited
false
null
0
undefined
""
NaN

I'm out. What are the last 2?

Collapse
 
joshcheek profile image
Josh Cheek

Shoutout! I didn't get NaN

Collapse
 
kenbellows profile image
Ken Bellows

-NaN and -0

Collapse
 
eljayadobe profile image
Eljay-Adobe

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