DEV Community

Cover image for 10 Challenging JavaScript Quiz Questions and Answers

10 Challenging JavaScript Quiz Questions and Answers

Nick Scialli (he/him) on August 27, 2020

The following questions are intended to be challenging and instructive. If you know exactly how to answer each one, that's great, but if you get so...
Collapse
 
pentacular profile image
pentacular

identity (===)

Just noting that === is not an identity operator -- it's the strict equality comparison operator.

We can easily tell that it is not an identity operator because, e.g., NaN === NaN is false.

b and a are pointing to the same

It's a bit problematic to say this, since Javascript doesn't have pointers.

It's sufficient to say that a and b have the same value, so they are the same object, and so provide access to the same properties. The rest then follows as you've said.

Collapse
 
objectmatrix profile image
Ahm A • Edited

the equality operator (==) will attempt to make the data types the same before proceeding

the identity operator (===) requires both data types to be the same, as a prerequisite.

Collapse
 
pentacular profile image
pentacular

We can tell that === is not an identity operator since it may return false when comparing a value with itself.

e.g. NaN === NaN.

Thread Thread
 
objectmatrix profile image
Ahm A • Edited

NaN === NaN // false, The identity evaluation algorithm (IEA) rule 4
Operands are the same type (numbers), but the IEA rule 4 indicates than nothing is equal with a NaN.
The result is false.

The identity evaluation algorithm (IEA) ===:

1. If both operands have different types, they are not strictly equal
2. If both operands are null, they are strictly equal
3. If both operands are undefined, they are strictly equal
4. If one or both operands are NaN, they are not strictly equal
5. If both operands are true or both false, they are strictly equal
6. If both operands are numbers and have the same value, they are strictly equal
7. If both operands are strings and have the same value, they are strictly equal
8. If both operands have reference to the same object or function, they are strictly equal
9. In all other cases operands are not strictly equal.
Enter fullscreen mode Exit fullscreen mode

Also,NaNs are the only non-reflexive value, i.e., if x !== x, then x is a NaN

more details:
dmitripavlutin.com/the-legend-of-j...

Thread Thread
 
pentacular profile image
pentacular

The page you've referenced is mostly correct, but they're a bit confused on terminology, and have made things rather more complicated that necessary.

See: ecma-international.org/publication...

7.2.16 Strict Equality Comparison
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number or BigInt, then a. Return ! Type(x)::equal(x, y).
    1. Return ! SameValueNonNumeric(x, y).

Now, what this means is that some things with the same identity compare false with ===, and some things with different identities compare true with ===.

e.g.

NaN === NaN is false

-0 === 0 is true

Which means that it is not checking the identity of the operands -- it is computing a kind of equality (using the strict equality comparison).

So, === is not an identity operator, and for these reasons is not called an identity operator in the language specification.

It's just some confused people on the internet who are making that into a popular mistake. :)

Collapse
 
thgh profile image
Thomas Ghysels

Some instructive feedback for question 10: initialize greatest to -Infinity (or arr[0])

Collapse
 
cayhorstmann profile image
cayhorstmann

These are very nice questions that probe JavaScript fundamentals and not ephemeral trivia. I suggest you change #4 and #6 so that they don't require knowledge about the behavior of == vs. ===. (Nobody should use == with JavaScript in 2020.) If you think that makes #4 too easy, perhaps you could add whether Object.keys(obj).map(x => parseInt(x)) === Object.values(obj). And as a bonus puzzler, why isn't Object.keys(obj).map(parseInt) the same as Object.keys(obj).map(x => parseInt(x))?

Collapse
 
voiedev profile image
Charles Allen

Love the bonus question! That's quite the trap.

Collapse
 
pris_stratton profile image
pris stratton

Very cool I love these. I am going to try the quiz in the link =)

Collapse
 
dotnetcarpenter profile image
Jon Ege Ronnenberg • Edited

Nice quiz. I enjoyed it more than other JS quizzes I have seen.
In Question 2 though, since it is about performance, it might be useful to know that a var declaration in a for loop is more performant than a let declaration. As long as your for loop is scoped to a function, you should not get any nasty surprises.

An example could be:

function each (f, array) {
  for (var i = 0, max = array.length; i < max; ++i) {
    f(array[i], i);
  }
}
Collapse
 
madza profile image
Madza
Collapse
 
naresh profile image
Naresh Poonia

I'm learning Javascript, I have bookmarked this post.
I would like to come here later and check how many I can answer.
Thank you for this post

Collapse
 
alin11 profile image
Ali Nazari
Collapse
 
yogeshdharya profile image
YogeshDharya

Hey Nick ! I really liked the blog and wanted to tell you that as of now(16th March 2023 22:02 IST) my Like made your existing likes sum up to 200 . Than u so much for your work . Kindly keep Sharing your knowledge with us

Collapse
 
alin11 profile image
Ali Nazari

Nice. Learnt many things. Thanks!

Collapse
 
eliakorkmaz profile image
Emrah Korkmaz

Cool post Nick, I've subscribed to your newsletter to getting some quality posts 🤟

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Nice article. I like this kind of challenge because it helps me to keep my mind up to date with JS knowledge. ✨

Collapse
 
bernardbaker profile image
Bernard Baker

That was fun. I got them all right. Great way to spend 5 mins post tea time.