forked from leonardomso/33-js-concepts
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
963a6a8
commit 3e0e125
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function ceo() { | ||
console.log("CEO: Let's make a dent in the universe"); | ||
cto(); | ||
} | ||
|
||
function cto() { | ||
console.log("CTO: Let's make a gamechanging product"); | ||
engineer(); | ||
} | ||
|
||
function engineer() { | ||
console.log("Engineer: Let's make a new js framework"); | ||
techLead(); | ||
} | ||
|
||
function techLead() { | ||
console.log("Tech Lead: Let's ditch Google, Amazon, Facebook"); | ||
developer(); | ||
} | ||
function developer() { | ||
console.log("Developer: Let's copy Stackoverflow"); | ||
throw new Error("Developer Error"); | ||
} | ||
ceo(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Data Types | ||
1. Number | ||
2. String | ||
3. Boolean | ||
4. Null | ||
5. Undefined | ||
6. ~~Object => Not primitive data type~~ | ||
|
||
## Null vs Undefined | ||
A variable that has not been assigned a value is of type undefined. | ||
|
||
A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned. | ||
|
||
When checking for null or undefined, beware of the differences between equality (==) and identity (===) operators, as the former performs type-conversion. | ||
|
||
```JavaScript | ||
typeof null // "object" (not "null" for legacy reasons) | ||
typeof undefined // "undefined" | ||
null === undefined // false | ||
null == undefined // true | ||
null === null // true | ||
null == null // true | ||
!null // true | ||
isNaN(1 + null) // false | ||
isNaN(1 + undefined) // true | ||
``` | ||
|
||
|
||
[Difference between null and undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null#Difference_between_null_and_undefined) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 3. Value Types and Reference Types | ||
|
||
In JavaScript we have types that are copied by value and types copied by reference. | ||
|
||
>Primitive types are **`copied by value`** | ||
>Object types are **`copied by reference`** | ||
##### Primitive types | ||
- null | ||
- undefined | ||
- Number | ||
- String | ||
- Boolean | ||
|
||
##### Object types | ||
- Object | ||
- Array | ||
- Function | ||
- ... | ||
|
||
Primitive Example | ||
|
||
```JavaScript | ||
var a = 5; | ||
var b = a; | ||
a = 10; | ||
console.log(a); // 10 | ||
console.log(b); // 5 | ||
// this is also true for string, boolean, null, undefined | ||
|
||
``` | ||
b is still 5 even though the value of a has changed. | ||
The value is **copied** | ||
|
||
Object Example | ||
|
||
```JavaScript | ||
var a = {}; | ||
var b = a; | ||
a.a = 1; | ||
console.log(a); // {a: 1} | ||
console.log(b); // {a: 1} | ||
|
||
``` | ||
|
||
Array Example | ||
```JavaScript | ||
var a = []; | ||
var b = a; | ||
a.push(1); | ||
console.log(a); // [1] | ||
console.log(b); // [1] | ||
console.log(a === b); // true | ||
``` | ||
|
||
When we assign Objects (non-primitives) to the variable, we copy them by reference. | ||
|
||
Advanced | ||
|
||
When we compare Objects, equality operator (===) will check if they point to the same address. | ||
|
||
```JavaScript | ||
console.log([10] === [10]); // false | ||
``` | ||
Since the first array [10] and the second array [10] are different array(has different references), this will return false. | ||
|
||
[Article Link](https://medium.com/dailyjs/back-to-roots-javascript-value-vs-reference-8fb69d587a18) |