-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
85 lines (63 loc) · 3.52 KB
/
index.md
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
---
title: Falsy
slug: Glossary/Falsy
page-type: glossary-definition
---
{{GlossarySidebar}}
A **falsy** (sometimes written **falsey**) value is a value that is considered false when encountered in a {{Glossary("Boolean")}} context.
{{Glossary("JavaScript")}} uses {{Glossary("Type_Conversion", "type conversion")}} to coerce any value to a Boolean in contexts that require it, such as {{Glossary("Conditional", "conditionals")}} and {{Glossary("Loop", "loops")}}.
The following table provides a complete list of JavaScript falsy values:
| Value | Type | Description |
| --------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| {{Glossary("null")}} | Null | The keyword [`null`](/en-US/docs/Web/JavaScript/Reference/Operators/null) — the absence of any value. |
| {{Glossary("undefined")}} | Undefined | [`undefined`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined) — the primitive value. |
| `false` | Boolean | The keyword [`false`](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words). |
| {{Glossary("NaN")}} | Number | [`NaN`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN) — not a number. |
| `0` | Number | The {{jsxref("Number")}} zero, also including `0.0`, `0x0`, etc. |
| `-0` | Number | The {{jsxref("Number")}} negative zero, also including `-0.0`, `-0x0`, etc. |
| `0n` | BigInt | The {{jsxref("BigInt")}} zero, also including `0x0n`, etc. Note that there is no {{jsxref("BigInt")}} negative zero — the negation of `0n` is `0n`. |
| `""` | String | Empty [string](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) value, also including `''` and ` `` `. |
| {{domxref("document.all")}} | Object | The only falsy object in JavaScript is the built-in {{domxref("document.all")}}. |
The values `null` and `undefined` are also [nullish](/en-US/docs/Glossary/Nullish).
## Examples
Examples of _falsy_ values in JavaScript (which are coerced to false in Boolean contexts, and thus _bypass_ the `if` block):
```js
if (false) {
// Not reachable
}
if (null) {
// Not reachable
}
if (undefined) {
// Not reachable
}
if (0) {
// Not reachable
}
if (-0) {
// Not reachable
}
if (0n) {
// Not reachable
}
if (NaN) {
// Not reachable
}
if ("") {
// Not reachable
}
```
### The logical AND operator, &&
If the first object is falsy, it returns that object:
```js
console.log(false && "dog");
// ↪ false
console.log(0 && "dog");
// ↪ 0
```
## See also
- Related glossary terms:
- {{Glossary("Truthy")}}
- {{Glossary("Type_coercion", "Coercion")}}
- {{Glossary("Boolean")}}
- [Boolean coercion](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion)