Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-creates committed Jan 6, 2021
1 parent 963a6a8 commit 3e0e125
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/#1 Call Stack/index.js
@@ -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();
29 changes: 29 additions & 0 deletions src/#2 Primitive Types/README.md
@@ -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 added src/#2 Primitive Types/index.js
Empty file.
68 changes: 68 additions & 0 deletions src/#3 Value Types and Reference Types/README.md
@@ -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)

0 comments on commit 3e0e125

Please sign in to comment.