Skip to content

Commit db34095

Browse files
Yash-Handafhemberger
authored andcommitted
Update what-are-truthy-and-falsy-values.md (#2078)
1 parent fb5a2bf commit db34095

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

locale/en/knowledge/javascript-conventions/what-are-truthy-and-falsy-values.md

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tags:
55
- falsy
66
- types
77
- coercion
8-
title: 'What are "truthy" and "falsy" values?'
8+
title: 'What are "truthy" and "falsy" values?'
99
difficulty: 4
1010
layout: knowledge-post.hbs
1111
---
@@ -15,60 +15,68 @@ JavaScript is weakly typed language. That means different types can be
1515
used in operations and the language will try to convert the types
1616
until the operation makes sense.
1717

18-
console.log("1" > 0); // true, "1" converted to number
19-
console.log(1 + "1"); // 11, 1 converted to string
18+
```js
19+
console.log("1" > 0); // true, "1" converted to number
20+
console.log(1 + "1"); // 11, 1 converted to string
21+
```
2022

21-
Type conversion also applys when values are used in unary boolean
23+
Type conversion also applies when values are used in unary boolean
2224
operations, most notably if statements. If a value converts to the
2325
boolean true, then it is said to be "truthy". If it converts to false
2426
it is "falsy".
2527

26-
var myval = "value";
27-
if(myval) {
28-
console.log("This value is truthy");
29-
}
28+
```js
29+
let myval = "value";
30+
if(myval) {
31+
console.log("This value is truthy");
32+
}
3033

31-
myval = 0;
32-
if(!myval) {
33-
console.log("This value is falsy");
34-
}
34+
myval = 0;
35+
if(!myval) {
36+
console.log("This value is falsy");
37+
}
38+
```
3539

3640
Since most values in javascript are truthy, e.g. objects, arrays, most
3741
numbers and strings, it's easier to identify all of the falsy
3842
values. These are:
3943

40-
false // obviously
41-
0 // The only falsy number
42-
"" // the empty string
43-
null
44-
undefined
45-
NaN
44+
```js
45+
false // obviously
46+
0 // The only falsy number
47+
"" // the empty string
48+
null
49+
undefined
50+
NaN
51+
```
4652

4753
Note that all objects and arrays are truthy, even empty ones.
4854

4955
Truthiness and Falsiness also come into play with logical
5056
operators. When using logical AND/OR, the values will be converted
51-
based on truthiness or falsyness and then the expression will resolve
57+
based on truthiness or falseness and then the expression will resolve
5258
to the last truthy value. Short circuit rules apply. Here's an
5359
extended example.
5460

55-
var first = "truthy"
56-
, second = "also truthy";
61+
```js
62+
let first = "truthy",
63+
second = "also truthy";
5764

58-
var myvalue = first && second;
59-
console.log(myvalue); // "also truthy"
65+
let myvalue = first && second;
66+
console.log(myvalue); // "also truthy"
6067

61-
first = null;
62-
second = "truthy";
68+
first = null;
69+
second = "truthy";
6370

64-
myvalue = first || second;
65-
console.log(myvalue); // "truthy"
71+
myvalue = first || second;
72+
console.log(myvalue); // "truthy"
6673

67-
myvalue2 = second || first;
68-
console.log(myvalue2); // "truthy"
74+
myvalue2 = second || first;
75+
console.log(myvalue2); // "truthy"
6976

70-
var truthy = "truthy"
71-
, falsy = 0;
77+
let truthy = "truthy",
78+
falsy = 0;
7279

73-
myvalue = truthy ? true : false;
74-
myvalue = falsy ? true : false;
80+
myvalue = truthy ? true : false;
81+
myvalue = falsy ? true : false;
82+
```

0 commit comments

Comments
 (0)