From 3e0e12543e693d2b267405370506dca1e8997c7a Mon Sep 17 00:00:00 2001 From: ben-creates Date: Wed, 6 Jan 2021 09:44:37 +0900 Subject: [PATCH] #1 ~ #3 --- src/#1 Call Stack/index.js | 24 +++++++ src/#2 Primitive Types/README.md | 29 ++++++++ src/#2 Primitive Types/index.js | 0 .../README.md | 68 +++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/#1 Call Stack/index.js create mode 100644 src/#2 Primitive Types/README.md create mode 100644 src/#2 Primitive Types/index.js create mode 100644 src/#3 Value Types and Reference Types/README.md diff --git a/src/#1 Call Stack/index.js b/src/#1 Call Stack/index.js new file mode 100644 index 00000000..c976cb42 --- /dev/null +++ b/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(); diff --git a/src/#2 Primitive Types/README.md b/src/#2 Primitive Types/README.md new file mode 100644 index 00000000..794b8c15 --- /dev/null +++ b/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) diff --git a/src/#2 Primitive Types/index.js b/src/#2 Primitive Types/index.js new file mode 100644 index 00000000..e69de29b diff --git a/src/#3 Value Types and Reference Types/README.md b/src/#3 Value Types and Reference Types/README.md new file mode 100644 index 00000000..eb62e97e --- /dev/null +++ b/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)