Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce JS-Dev-101 Exam - Topic 1 Question 8 Discussion

Given a value, which two options can a developer use to detect if the value is NaN?
C) isNaN(value) and D) Object.is(value, NaN)
A) value === Number.NaN
B) value == NaN

Salesforce JS-Dev-101 Exam - Topic 1 Question 8 Discussion

Actual exam question for Salesforce's JS-Dev-101 exam
Question #: 8
Topic #: 1
[All JS-Dev-101 Questions]

Given a value, which two options can a developer use to detect if the value is NaN?

Show Suggested Answer Hide Answer
Suggested Answer: C, D

Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:

We already know NaN is special: it is not equal to itself.

Check each:

A . value === Number.NaN

Number.NaN is NaN.

NaN === NaN is always false.

This will never be true; cannot reliably detect NaN.

B . value == NaN

NaN == NaN is also always false.

Again, this never detects NaN.

C . isNaN(value)

Global isNaN converts its argument to a number and then checks if the result is NaN.

This can detect NaN, but it may also return true for non-number values that coerce to NaN, such as isNaN('foo').

Regardless, it is a standard way to detect if a value is ''NaN-like'' in JavaScript.

D . Object.is(value, NaN)

Object.is(NaN, NaN) returns true.

This is a strict way to detect a value that is exactly NaN (no coercion).

Therefore, among the given choices, the two viable ways to detect NaN are:

isNaN(value)

Object.is(value, NaN)

________________________________________


Contribute your Thoughts:

0/2000 characters
Lanie
4 days ago
D) Object.is(value, NaN) works too!
upvoted 0 times
...
Erick
9 days ago
C) isNaN(value) is the way to go!
upvoted 0 times
...
Leontine
14 days ago
I feel like both B and D are incorrect, but I can't remember why exactly.
upvoted 0 times
...
Brett
19 days ago
I practiced a similar question, and I think Object.is(value, NaN) might be valid too, but I can't recall the details.
upvoted 0 times
...
Chara
24 days ago
I think value === Number.NaN won't work because NaN is not equal to itself, right?
upvoted 0 times
...
Annelle
2 months ago
I remember that using isNaN(value) is a common way to check for NaN, but I'm not sure about the others.
upvoted 0 times
...

Save Cancel