Skip to content

Commit ddcd83b

Browse files
committed
null vs undefined
1 parent 9ad15a0 commit ddcd83b

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

Javascript/Tricky-JS-Problems/null-vs-undefined.md

+42-10
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44

55
**Answer**: JavaScript has two distinct values for nothing, null and undefined.
66

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

1013
### 8 Ways to get Undefined:
1114

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
2023

2124
### null
2225

@@ -25,3 +28,32 @@ null means empty or non-existent value which is used by programmers to indicate
2528
Btw, null == undefined
2629

2730
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+
let logHi = (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:
58+
logHi();
59+
// hi

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ A collection of super-popular Interview questions, along with explanations and i
184184
- [why-does-adding-two-decimals-in-javascript-produce-a-wrong-result](Javascript/Tricky-JS-Problems/why-does-adding-two-decimals-in-javascript-produce-a-wrong-result.md)
185185
- [typeof-NaN](Javascript/Tricky-JS-Problems/typeof-NaN.md)
186186
- [If null is a primitive, why does typeof(null) return "object"?](Javascript/Tricky-JS-Problems/typeof-null-why-its-object.md)
187+
- [null-vs-undefined](Javascript/Tricky-JS-Problems/null-vs-undefined.md)
187188

188189
## Most common Async/Await and Promise related Interview Topics & Questions
189190

0 commit comments

Comments
 (0)