Skip to content

Commit cff619a

Browse files
committed
Updates data-types/54-other-primitive-data-types.md
Auto commit by GitBook Editor
1 parent 0dc95c3 commit cff619a

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

data-types/54-other-primitive-data-types.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ JavaScript also gives us a few Data Types that are used in more specific ways: B
66

77
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.
88

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.
1010

1111
## Undefined
1212

@@ -15,6 +15,7 @@ Boolean values are generally checked within a conditional (an `if` statement) or
1515
```js
1616
let foo;
1717
```
18+
1819
The following example indicates how this could be used in code:
1920

2021
```js
@@ -23,36 +24,37 @@ if (typeof(foo) === 'undefined') {
2324
// Code here would execute because foo is undefined.
2425
}
2526
```
27+
2628
This is the default type for any variable that has not been assigned a value.
2729

2830
## Null
2931

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.
3133

3234
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.
3335

3436
Here's an example of using `null` in code. Imagine we are writing a system trying to manage a `currentUser` object:
3537

3638
```js
3739
let currentUser;
38-
if (typeof(foo) === 'undefined') {
40+
if (typeof(currentUser) === 'undefined') {
3941
// This code executes because foo has not been assigned a value.
4042
}
4143
```
44+
4245
Later on in our code, we might want to set the `currentUser` back to `null` to indicate no user is logged in:
4346

4447
```js
4548
currentUser = null;
46-
if (typeof(foo) === 'undefined') {
49+
if (typeof(currentUser) === 'undefined') {
4750
// This code will not execute because currentUser is equal to null.
48-
} else if (typeof(foo) === 'null') {
51+
} else if (typeof(currentUser) === 'null') {
4952
// This code will execute.
5053
// Do something to login the user
5154
}
52-
5355
```
54-
Now, when we do the check against `'undefined'` it's `false` because `currentUser` has been defined as `'null'`.
5556

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'`.
5758

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.
5860

0 commit comments

Comments
 (0)