Skip to content

Commit 9ad15a0

Browse files
committed
null vs undefined
1 parent 2e7938c commit 9ad15a0

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

Javascript/Tricky-JS-Problems/Collection-of-Tricky-JS-Questlions.md

+93
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,96 @@ Object.freeze(person)
382382
person.name = "Lima" //TypeError: Cannot assign to read only property 'name' of object
383383
console.log(person)
384384
```
385+
386+
#### [More explanations](https://stackoverflow.com/a/51858981/1902852)
387+
388+
## Summary:
389+
390+
`const` and `Object.freeze()` serve totally different purposes.
391+
392+
- `const` is there for declaring a variable which has to assinged right away and can't be reassigned. variables declared by `const` are block scoped and not function scoped like variables declared with `var`
393+
- `Object.freeze()` is a method which accepts an object and returns the same object. Now the object cannot have any of its properties removed or any new properties added.
394+
395+
## Examples `const`:
396+
397+
**Example 1: Can't reassign `const`**
398+
399+
```
400+
const foo = 5;
401+
402+
foo = 6;
403+
404+
```
405+
406+
The following code throws an error because we are trying to reassign the variable foo who was declared with the `const` keyword, we can't reassign it.
407+
408+
**Example 2: Data structures which are assigned to `const` can be mutated**
409+
410+
```
411+
const object = {
412+
prop1: 1,
413+
prop2: 2
414+
}
415+
416+
object.prop1 = 5; // object is still mutable!
417+
object.prop3 = 3; // object is still mutable!
418+
419+
console.log(object); // object is mutated
420+
421+
422+
```
423+
424+
In this example we declare a variable using the `const` keyword and assign an object to it. Although we can't reassign to this variable called object, we can mutate the object itself. If we change existing properties or add new properties this will this have effect. To disable any changes to the object we need `Object.freeze()`.
425+
426+
## Examples `Object.freeze()`:
427+
428+
**Example 1: Can't mutate a frozen object**
429+
430+
<!-- begin snippet: js hide: false console: true babel: false -->
431+
432+
<!-- language: lang-js -->
433+
434+
object1 = {
435+
prop1: 1,
436+
prop2: 2
437+
}
438+
439+
object2 = Object.freeze(object1);
440+
441+
console.log(object1 === object2); // both objects are refer to the same instance
442+
443+
object2.prop3 = 3; // no new property can be added, won't work
444+
445+
delete object2.prop1; // no property can be deleted, won't work
446+
447+
console.log(object2); // object unchanged
448+
449+
<!-- end snippet -->
450+
451+
In this example when we call `Object.freeze()` and give `object1` as an argument the function returns the object which is now 'frozen'. If we compare the reference of the new object to the old object using the `===` operator we can observe that they refer to the same object. Also when we try to add or remove any properties we can see that this does not have any effect (will throw error in strict mode).
452+
453+
**Example 2: Objects with references aren't fully frozen**
454+
455+
<!-- begin snippet: js hide: false console: true babel: false -->
456+
457+
<!-- language: lang-js -->
458+
459+
const object = {
460+
prop1: 1,
461+
nestedObj: {
462+
nestedProp1: 1,
463+
nestedProp2: 2,
464+
}
465+
}
466+
467+
468+
const frozen = Object.freeze(object);
469+
470+
frozen.prop1 = 5; // won't have any effect
471+
frozen.nestedObj.nestedProp1 = 5; //will update because the nestedObject isn't frozen
472+
473+
console.log(frozen);
474+
475+
<!-- end snippet -->
476+
477+
This example shows that the properties of nested objects (and other by reference data structures) are **still mutable**. So `Object.freeze()` doesn't fully 'freeze' the object when it has properties which are references (to e.g. Arrays, Objects).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
### null vs undefined
2+
3+
#### Question: What are the differences between null and undefined?
4+
5+
**Answer**: JavaScript has two distinct values for nothing, null and undefined.
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.
9+
10+
### 8 Ways to get Undefined:
11+
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
20+
21+
### null
22+
23+
null means empty or non-existent value which is used by programmers to indicate “no value”. null is a primitive value and you can assign null to any variable. null is not an object, it is a primitive value. For example, you cannot add properties to it. Sometimes people wrongly assume that it is an object, because typeof null returns "object".
24+
25+
Btw, null == undefined
26+
27+
ref: [history of typeof null](http://www.2ality.com/2013/10/typeof-null.html)

0 commit comments

Comments
 (0)