aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html
blob: 6f27945de597ecdb2528fcca46f388d28d41e0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
title: Number.isNaN()
slug: Web/JavaScript/Reference/Global_Objects/Number/isNaN
translation_of: Web/JavaScript/Reference/Global_Objects/Number/isNaN
---
<div>{{JSRef}}</div>

<p>The <strong><code>Number.isNaN()</code></strong> method determines whether the passed value is {{jsxref("NaN")}} and its type is {{jsxref("Number")}}. It is a more robust version of the original, global {{jsxref("isNaN", "isNaN()")}}.</p>

<div>{{EmbedInteractiveExample("pages/js/number-isnan.html", "taller")}}</div>



<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox"><code>Number.isNaN(<var>value</var>)</code></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>The value to be tested for {{jsxref("NaN")}}.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p><strong>true</strong> if the given value is {{jsxref("NaN")}} and its type is {{jsxref("Number")}}; otherwise, <strong>false</strong>.</p>

<h2 id="Description">Description</h2>

<p>Due to both equality operators, {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} and {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}}, evaluating to <code>false</code> when checking if {{jsxref("NaN")}} <em>is</em> {{jsxref("NaN")}}, the function <code>Number.isNaN()</code> has become necessary. This situation is unlike all other possible value comparisons in JavaScript.</p>

<p>In comparison to the global {{jsxref("isNaN", "isNaN()")}} function, <code>Number.isNaN()</code> doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to {{jsxref("NaN")}}, but aren't actually the same value as {{jsxref("NaN")}}. This also means that only values of the type number, that are also {{jsxref("NaN")}}, return <code>true</code>.</p>

<h2 id="Examples">Examples</h2>

<pre class="brush: js">Number.isNaN(NaN);        // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0);      // true

// e.g. these would have been true with global isNaN()
Number.isNaN('NaN');      // false
Number.isNaN(undefined);  // false
Number.isNaN({});         // false
Number.isNaN('blabla');   // false

// These all return false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN('37');
Number.isNaN('37.37');
Number.isNaN('');
Number.isNaN(' ');
</pre>

<h2 id="Polyfill">Polyfill</h2>

<p>The following works because NaN is the only value in javascript which is not equal to itself.</p>

<pre class="brush: js">Number.isNaN = Number.isNaN || function(value) {
    return value !== value;
}
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-number.isnan', 'Number.isnan')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-number.isnan', 'Number.isnan')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{Compat("javascript.builtins.Number.isNaN")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Number")}}</li>
 <li>{{jsxref("isNaN", "isNaN()")}}</li>
</ul>