You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: data-types/54-other-primitive-data-types.md
+10-8
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ JavaScript also gives us a few Data Types that are used in more specific ways: B
6
6
7
7
The Boolean Data Type has a value that is either `true` or `false`. These types of variables are helpful when controlling the logical flow of a program. We often use different logic to determine a `true` or `false` so that we can later have an easy check to determine the next commands the program should execute.
8
8
9
-
Boolean values are generally checked within a conditional (an `if` statement) or to control the execution of a `while` loop. These will be used throughout our code.
9
+
Boolean values are generally checked within a conditional \(an `if` statement\) or to control the execution of a `while` loop. These will be used throughout our code.
10
10
11
11
## Undefined
12
12
@@ -15,6 +15,7 @@ Boolean values are generally checked within a conditional (an `if` statement) or
15
15
```js
16
16
let foo;
17
17
```
18
+
18
19
The following example indicates how this could be used in code:
19
20
20
21
```js
@@ -23,36 +24,37 @@ if (typeof(foo) === 'undefined') {
23
24
// Code here would execute because foo is undefined.
24
25
}
25
26
```
27
+
26
28
This is the default type for any variable that has not been assigned a value.
27
29
28
30
## Null
29
31
30
-
[Null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) is a Data Type that is used to indicate there is nothing assigned to the variable. At first, it seems as if it's a duplicate of Undefined, but in fact it's different in an important way. We would probably never assign `undefined` as a value to a variable in our system. Remember, it is the _default_ type of a non-initialized variable.
32
+
[Null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) is a Data Type that is used to indicate there is nothing assigned to the variable. At first, it seems as if it's a duplicate of Undefined, but in fact it's different in an important way. We would probably never assign `undefined` as a value to a variable in our system. Remember, it is the _default_ type of a non-initialized variable.
31
33
32
34
But imagine the case where we have some variable where there _could_ be a value assigned, but at some point in our program it _does not_ have a value assigned. This is where the Null data type becomes helpful. We can set the value of a variable to `null` to indicate the variable does not currently point to a value. This is used often in API design to indicate that where there _could_ be information it is missing.
33
35
34
36
Here's an example of using `null` in code. Imagine we are writing a system trying to manage a `currentUser` object:
35
37
36
38
```js
37
39
let currentUser;
38
-
if (typeof(foo) ==='undefined') {
40
+
if (typeof(currentUser) ==='undefined') {
39
41
// This code executes because foo has not been assigned a value.
40
42
}
41
43
```
44
+
42
45
Later on in our code, we might want to set the `currentUser` back to `null` to indicate no user is logged in:
43
46
44
47
```js
45
48
currentUser =null;
46
-
if (typeof(foo) ==='undefined') {
49
+
if (typeof(currentUser) ==='undefined') {
47
50
// This code will not execute because currentUser is equal to null.
48
-
} elseif (typeof(foo) ==='null') {
51
+
} elseif (typeof(currentUser) ==='null') {
49
52
// This code will execute.
50
53
// Do something to login the user
51
54
}
52
-
53
55
```
54
-
Now, when we do the check against `'undefined'` it's `false` because `currentUser` has been defined as `'null'`.
55
56
56
-
It's common for developers to use `null` to represent missing data. Imagine an object that stores an address. We could picture that object with attributes for both "street address" and "street address line two". However, many people do not have an additional street address line to use in their addresses. In this case, the optional "street address line two" might be equal to `null` to represent that value is missing in a purposeful way.
57
+
Now, when we do the check against `'undefined'` it's `false` because `currentUser` has been defined as `'null'`.
57
58
59
+
It's common for developers to use `null` to represent missing data. Imagine an object that stores an address. We could picture that object with attributes for both "street address" and "street address line two". However, many people do not have an additional street address line to use in their addresses. In this case, the optional "street address line two" might be equal to `null` to represent that value is missing in a purposeful way.
0 commit comments