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: Javascript/Tricky-JS-Problems/null-vs-undefined.md
+42-10
Original file line number
Diff line number
Diff line change
@@ -4,19 +4,22 @@
4
4
5
5
**Answer**: JavaScript has two distinct values for nothing, null and undefined.
6
6
7
-
undefined
8
-
undefined means, value of the variable is not defined. JavaScript has a global variable undefined whose value is "undefined" and typeof undefined is also "undefined". Remember, undefined is not a constant or a keyword. undefined is a type with exactly one value: undefined. Assigning a new value to it does not change the value of the type undefined.
7
+
### undefined
8
+
9
+
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as: JavaScript has a global variable undefined whose value is "undefined" and typeof undefined is also "undefined".
10
+
11
+
Remember, undefined is not a constant or a keyword. undefined is a type with exactly one value: undefined. Assigning a new value to it does not change the value of the type undefined.
9
12
10
13
### 8 Ways to get Undefined:
11
14
12
-
A declared variable without assigning any value to it.
13
-
Implicit returns of functions due to missing return statements.
14
-
return statements that do not explicitly return anything.
15
-
Lookups of non-existent properties in an object.
16
-
Function parameters that have not passed.
17
-
Anything that has been set to the value of undefined.
18
-
Any expression in the form of void(expression)
19
-
The value of the global variable undefined
15
+
-A declared variable without assigning any value to it.
16
+
-Implicit returns of functions due to missing return statements.
17
+
-return statements that do not explicitly return anything.
18
+
-Lookups of non-existent properties in an object.
19
+
-Function parameters that have not passed.
20
+
-Anything that has been set to the value of undefined.
21
+
-Any expression in the form of void(expression)
22
+
-The value of the global variable undefined
20
23
21
24
### null
22
25
@@ -25,3 +28,32 @@ null means empty or non-existent value which is used by programmers to indicate
25
28
Btw, null == undefined
26
29
27
30
ref: [history of typeof null](http://www.2ality.com/2013/10/typeof-null.html)
31
+
32
+
From the preceding examples, it is clear that `undefined` and `null` are two distinct types: `undefined` is a type itself (undefined) while `null` is an object.
33
+
34
+
null === undefined // false
35
+
null == undefined // true
36
+
null === null // true
37
+
38
+
and
39
+
40
+
null = 'value' // ReferenceError
41
+
undefined = 'value' // 'value'
42
+
43
+
---
44
+
45
+
### Practical Differences
46
+
47
+
All of this is great, but what about a practical difference between null and undefined?
48
+
49
+
Consider the following code snippet:
50
+
51
+
```js
52
+
letlogHi= (str="hi") => {
53
+
console.log(str)
54
+
}
55
+
```
56
+
57
+
The code above creates a function named logHi. This function requires one parameter and sets the default of that parameter to hi if it isn’t supplied. Here’s what that looks like:
0 commit comments