JS-Dev-101 practice question 7 of 25
Given a value, which two options can a developer use to detect if the value is NaN?
Full set: 148 questionsIn JavaScript, NaN (Not-a-Number) has the unique property of not being equal to itself, so direct comparison (== or ===) fails. The global isNaN() function correctly identifies NaN, but it has a notable quirk: it coerces its argument to a number, so isNaN('hello') returns true. A more robust and modern method is Object.is(value, NaN), introduced in ES2015 (ES6). It determines if two values are the same without type coercion and correctly identifies NaN without the side effects of the global isNaN() function.
A. value === Number.NaN will always evaluate to false because NaN is never strictly equal to itself or any other value.
B. value == NaN will always evaluate to false because NaN is never loosely equal to itself or any other value.
1. ECMA-262, 11th Edition. (June 2020). ECMAScript® 2020 Language Specification. Section 7.2.14, "SameValueZero(x, y)" and Section 20.1.2.12, "Object.is ( value1, value2 )". (The specification details why Object.is(NaN, NaN) is true).
2. MDN Web Docs. (2023). NaN. Mozilla. Retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/GlobalObjects/NaN. (Section: "Testing against NaN").
3. MDN Web Docs. (2023). Object.is(). Mozilla. Retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/GlobalObjects/Object/is. (Section: "Description" and "Examples").
4. Haverbeke, M. (2018). Eloquent JavaScript (3rd ed.). No Starch Press. Chapter 1, "Values, Types, and Operators," Section "Special numbers".
Keep practicing
Take the free JS-Dev-101 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other JS-Dev-101 practice questions
- Question 1A developer executes: document.cookie; document.cookie = 'key=John Smith'; What is the be…
- Question 2A developer is required to write a function that calculates the sum of elements in an arr…
- Question 3Given the code below: 01 const delay = async delay => { 02 return new Promise((resolve, r…
- Question 401 function Animal(size, type) { 02 this.type = type || 'Animal'; 03 this.canTalk = false…
- Question 5Refer to the code snippet: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i …
- Question 6Code: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i < array.length; i++) …
