JavaScript Fundamentals Guide
JavaScript Fundamentals Guide
HTML
TO
REACT
The Ultimate Guide
NGNINJA
ACADEMY
NgNinja Academy | All Rights Reserved
JavaScript
Table Of Content
1 / 92
NgNinja Academy | All Rights Reserved
Filter
Module 3 - JavaScript Objects and Functions
JavaScript Object Basics
Access Object Value
JavaScript Functions
Example Function
Invoke Function
Local variables
Function Expressions
Scoping in JavaScript
Two Types
Examples
Example: JavaScript does not have block scope
Constructor Functions
The this keyword
this with example
More this examples
The new Operator
Understand with example
Example of creating an object with and without new operator
WITHOUT new operator
WITH new operator
Interview Question: What is the di erence between the new operator and [Link]
Operator
new Operator in JavaScript
[Link] in JavaScript
Module 4 - Prototypes and Prototypal Inheritance
JavaScript as Prototype-based language
What is a prototype?
Example of Prototype
What is Prototypal Inheritance?
Understand Prototypal Inheritance by an analogy
Why is Prototypal Inheritance better?
Example of Prototypal Inheritance
Linking the prototypes
Prototype Chain
How does prototypal inheritance/prototype chain work in above example?
Module 5 - Advanced JavaScript (Closures, Method Chaining, etc.)
Hoisting in JavaScript
Another example
We get an error with Function Expressions
JavaScript Closures
Closure remembers the environment
IIFE
What is happening here?
Closure And IIFE
2 / 92
NgNinja Academy | All Rights Reserved
3 / 92
NgNinja Academy | All Rights Reserved
What is JavaScript
4 / 92
NgNinja Academy | All Rights Reserved
It can be used on the Frontend, Backend, and also in the databases like MongoDB
It is dynamic in nature ex: objects and arrays can be of mixed types
5 / 92
NgNinja Academy | All Rights Reserved
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<script>
[Link]("Hello World");
</script>
</body>
</html>
JavaScript code is written in between the script tag in the above code.
When the page loads the browser will run the code between the script tag.
alert() function will be called which will create a model with hello world text on it.
Instead of creating your own HTML le you can use online IDE as a JavaScript playground
6 / 92
NgNinja Academy | All Rights Reserved
Code Sandbox
PlayCode
Type "echo"
Then everytime you want to JavaScript program hit hit cmd + shift + p on Mac, ctrl
+ shift + p on Windows / Linux
// [Link]
{
// See [Link]
// for the documentation about the [Link] format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
},
{
"label": "Show in console",
"type": "shell",
"osx": {
"command": "/usr/local/opt/node@10/bin/node ${file}"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
7 / 92
NgNinja Academy | All Rights Reserved
8 / 92
NgNinja Academy | All Rights Reserved
Variables
// More examples
9 / 92
NgNinja Academy | All Rights Reserved
Values used in your code can be of certain type - number or string for example
This type is called data type of the language
Data Types supported in JavaScript are: Number, String, Boolean, Function, Object,
Null, and Undefined
They are categorized as primitive or non-primitive data types
Check the illustration below
10 / 92
NgNinja Academy | All Rights Reserved
11 / 92
NgNinja Academy | All Rights Reserved
Basic Operators
Special Operators
12 / 92
NgNinja Academy | All Rights Reserved
1.
var x = 15 + 5 // 20
var y = "hi"
var z = x + y // 20hi
2.
13 / 92
NgNinja Academy | All Rights Reserved
class Car {
14 / 92
NgNinja Academy | All Rights Reserved
constructor(vehicle) {
this._vehicle = vehicle;
}
move() {
[Link]("drive", this._vehicle);
}
}
class Bike {
constructor(vehicle) {
this._vehicle = vehicle;
}
move() {
[Link]("ride", this._vehicle);
}
}
function getVehicle(vehicle) {
switch ([Link]) {
case "bike":
return new Bike(vehicle);
case "car":
return new Car(vehicle);
default:
break;
}
}
// this would create the appropriate vehicle using the above classes
let vehicle = getVehicle({
type: "bike",
});
vehicle = getVehicle({
type: "car",
});
15 / 92
NgNinja Academy | All Rights Reserved
Conditionals
16 / 92
NgNinja Academy | All Rights Reserved
// ...your code
if(some-condition == true) {
// execute some code
}
else {
// execute some other code
}
If Else Condition
17 / 92
NgNinja Academy | All Rights Reserved
var x = 10;
if(x == 10) {
[Link]("x is 10")
}
else if(x < 10) {
[Link]("x is less than 10)
}
else {
[Link]("x is greater than 10)
}
Ternary Operator
// using if else
if(x == 10) {
[Link]("x is 10")
}
else {
[Link]("x is NOT 10")
}
// using ternary
18 / 92
NgNinja Academy | All Rights Reserved
condition ? if-code : else-code is the syntax used for the ternary operator
Advanced Ternary
You can also nest the ternary operators if there are complex conditions
// using if else
condition ? nested-ternary : else-code - this is the syntax we used for the above-nested
ternary operation
You can go multiple levels deep into writing nested ternary operator
But it is recommended to keep the ternary operators as simple as possible to keep the code more
readable
19 / 92
NgNinja Academy | All Rights Reserved
Switch Statements
switch(x) {
case 10:
[Link]("x is 10")
break
case 20:
[Link]("x is 20")
break
default
[Link]("x is NOT 10 nor 20")
}
20 / 92
NgNinja Academy | All Rights Reserved
// falsy values
false
0 (zero)
"" (empty string)
null
undefined
NaN (a special Number value meaning Not-a-Number)
// truthy values
This concept is important because the inherent values can then be used in conditional logic
You don't have to do if(x == false) - you can just do if(!x)
21 / 92
NgNinja Academy | All Rights Reserved
if (x) {
// x is truthy
}
else {
// x is falsy
// it could be false, 0, "", null, undefined or NaN
}
22 / 92
NgNinja Academy | All Rights Reserved
For Loop
Loops are used to run the same code block again and again "for" given number of times
If the condition is true it will run the code inside the loop
23 / 92
NgNinja Academy | All Rights Reserved
It will continue running the code inside the loop until the condition does not meet anymore
After that the execution will come outside the loop and continue executing the rest of the code
Below code will iterate over an array and log all its items
For-In loop
It is similar to for loop but is used to iterate over an object instead of an array
24 / 92
NgNinja Academy | All Rights Reserved
For-Of loop
for(var x of items) {
[Link](x) // 1, 2, 3
}
While loop
This loop executed a block of code "while" the given condition is true
var i = 0
while (i < 10) {
[Link](i)
25 / 92
NgNinja Academy | All Rights Reserved
i++
}
NOTE: Remember to terminate the while condition properly. Or else the loop will go into in nity and
it might crash your browser.
Do-While loop
It is similar to the while loop except it executes the block of code rst and then checks for the
condition
This process will repeat until the condition is true
var i = 0
do {
[Link](i)
i++
} while (i < 10)
Tip: In my experience, I have rarely used this do-while. Most of the time you can get away with
using the for or the while loop.
26 / 92
NgNinja Academy | All Rights Reserved
27 / 92
NgNinja Academy | All Rights Reserved
Map
function getSquare(item) {
return item * item
}
In the above example getSquare method is called for each item in the numbers array
The method returns the square of each number
The result of the .map is a new array with square of each number
Reduce
Similarly to .map - .reduce calls the given method for each element in the array
The result of each method call is passed over to the next method call in the array
This result is called as accumulator
It can anything like a string, number or any object
You can also pass in an initial value of the accumulator as an optional argument
28 / 92
NgNinja Academy | All Rights Reserved
In the above example getSum method is called for each item in the numbers array
0 is passed as the initial value of the accumulator
result is the variable name of the accumulator
The above .reduce method adds each item in the array and stores that sum in the result
variable
Finally the result is returned to sumOfNumbers
Filter
function isGreaterThanTwo(item) {
return item > 2
}
29 / 92
NgNinja Academy | All Rights Reserved
In the above example isGreaterThanTwo method checks if the value of the given item is greater
than two
The result is a new array with only [3,4] items in it
30 / 92
NgNinja Academy | All Rights Reserved
Module 3 - JavaScript
Objects and Functions
const person = {
name: "foo",
age: 21
}
31 / 92
NgNinja Academy | All Rights Reserved
1.
[Link]([Link]) // foo
2.
[Link](person['age']) // 21
32 / 92
NgNinja Academy | All Rights Reserved
JavaScript Functions
Example Function
function addMe(a, b) {
return a + b // The function returns the sum of a and b
}
Invoke Function
33 / 92
NgNinja Academy | All Rights Reserved
Local variables
function addMe(a) {
let b = 2
return a + b
}
function addMe(a) {
let b = 2
return a + b
}
34 / 92
NgNinja Academy | All Rights Reserved
Function Expressions
Please note that the name of the function is assigned to the variable instead of the function
Result of the function remains the same
35 / 92
NgNinja Academy | All Rights Reserved
Scoping in JavaScript
Two Types
Local scope
Available locally to a "block" of code
Global scope
Available globally everywhere
JavaScript traditionally always had function scope. JavaScript recently added block scope as a
part of the new standard. You will learn about this in the Advanced JavaScript module.
Examples
// global scope
var a = 1;
36 / 92
NgNinja Academy | All Rights Reserved
function one() {
[Link](a); // 1
}
one(); // 1
two(2); // 2
three(); // 3
var a = 1
function four(){
if(true){
var a = 4
}
37 / 92
NgNinja Academy | All Rights Reserved
38 / 92
NgNinja Academy | All Rights Reserved
Constructor Functions
It is considered good practice to name constructor functions with an upper-case rst letter. It is not
required though.
function Person() {
[Link] = "John"
[Link] = 21
}
The this represents the object (or function) that “owns” the currently executing code.
this keyword references current execution context.
When a JavaScript function is invoked, a new execution context is created.
this in js is di erent than other languages because of how functions are handled
Functions are objects in JavaScript
So we can change the value of this keyword for every function call
39 / 92
NgNinja Academy | All Rights Reserved
The value of this depends on the object that the function is attached to
In the below example;
getMyAge function belongs to person object
So, [Link] represents the person object's age property
const person = {
name: "foo",
age: 21,
getMyAge: function() {
return [Link] // 21
}
}
In below example -
var foo = 10; statement declares foo variable on the window object
40 / 92
NgNinja Academy | All Rights Reserved
So, [Link] returns the value of foo variable on the window object - which is 10
var myObject = { foo : 20}; declares foo property which belongs to myObject object
[Link](myObject); statement simply makes myObject the owner of the print method
So, [Link] now returns the value of foo variable on the window object - which is 20
function print(){
[Link]([Link]);
}
41 / 92
NgNinja Academy | All Rights Reserved
// user-defined object
class Car {
constructor(name) {
[Link] = name;
}
}
42 / 92
NgNinja Academy | All Rights Reserved
function Car(name) {
[Link](this) // this points to myCar
[Link] = name;
}
this.A = 1; - value of this is unde ned so this statement will throw error
var t = Foo(); - value of t will be unde ned because Foo() function is not returning anything
43 / 92
NgNinja Academy | All Rights Reserved
var t = Foo();
[Link](t); // undefined
44 / 92
NgNinja Academy | All Rights Reserved
function Car() {
[Link](this) // this points to myCar
[Link] = "Honda";
}
[Link] in JavaScript
45 / 92
NgNinja Academy | All Rights Reserved
const Car = {
name: "Honda"
}
46 / 92
NgNinja Academy | All Rights Reserved
JavaScript does not contain "classes" that de nes a blueprint for the object, such as is found in C++
or Java
JavaScript uses functions as "classes"
Everything is an object in JavaScript
In JavaScript, objects de ne their own structure
This structure can be inherited by other objects at runtime
What is a prototype?
47 / 92
NgNinja Academy | All Rights Reserved
Example of Prototype
Prototype property allows you to add properties and methods to any object dynamically
function Animal(name) {
[Link] = name
}
[Link] = 10
48 / 92
NgNinja Academy | All Rights Reserved
In JavaScript object inherits from object - unlike class inheritance in C++ or Java
Prototypal inheritance means that if the property is not found in the original object itself
Then the property will be searched for in the object's parent prototype object.
Object literally links to other objects
Check out the illustration above and refer the code below
function Animal(name) {
[Link] = name;
}
[Link] = function () {
[Link]("move");
};
function Cat(name) {
[Link](this, name);
49 / 92
NgNinja Academy | All Rights Reserved
[Link] = function () {
[Link]("meow");
};
[Link] = [Link]([Link])
Now our new misty cat object will inherit all the properties on Animal and Cat object and also the
properties on [Link] and [Link]
50 / 92
NgNinja Academy | All Rights Reserved
You have exam, you need a pen, but you don't have a pen
You ask your friend if they have a pen, but the don't - but they are a good friend
So they ask their friend if they have a pen, they do!
That pen gets passed to you and you can now use it
The friendship is the prototype link between them!
It is simpler
Just create and extend objects
You don't worry about classes, interfaces, abstract classes, virtual base classes, constructor,
etc...
It is more powerful
You can "mimic" multiple inheritance by extending object from multiple objects
Just handpick properties and methods from the prototypes you want
It is dynamic
You can add new properties to prototypes after they are created
This also auto-adds those properties and methods to those object which are inherited from
this prototype
It is less verbose than class-based inheritance
function Building(address) {
[Link] = address
}
51 / 92
NgNinja Academy | All Rights Reserved
[Link] = function() {
return [Link]
}
[Link] = function() {
return [Link]
}
[Link](myHome)
// Home {address: "1 Baker Street", owner: "Joe", constructor: Object}
[Link]([Link]) // Joe
[Link]([Link]) // 1 Baker Street
// On Building constructor
[Link] = function() {
return [Link]
}
// On Home constructor
[Link] = function() {
return [Link]
}
[Link]([Link]()) // Joe
[Link]([Link]()) // ERROR: [Link] is not a
function
52 / 92
NgNinja Academy | All Rights Reserved
[Link] = [Link]([Link])
[Link]([Link]()) // Joe
[Link]([Link]()) // 1 Baker Street
53 / 92
NgNinja Academy | All Rights Reserved
Prototype Chain
54 / 92
NgNinja Academy | All Rights Reserved
Module 5 - Advanced
JavaScript (Closures,
Method Chaining, etc.)
Hoisting in JavaScript
55 / 92
NgNinja Academy | All Rights Reserved
var bar = 1
Another example
// Function declarations
foo() // 1
function foo() {
[Link](1)
}
The variable declarations are silently moved to the very top of the current scope
Functions are hoisted rst, and then variables
But, this does not mean that assigned values (in the middle of function) will still be associated with
the variable from the start of the function
It only means that the variable name will be recognized starting from the very beginning of the
function
That is the reason, bar is undefined in this example
// Variable declarations
[Link](bar) // undefined
var bar = 1
56 / 92
NgNinja Academy | All Rights Reserved
NOTE 1: Variables and constants declared with let or const are not hoisted!
NOTE 2: Function declarations are hoisted - but function expressions are not!
// NO ERROR
foo();
function foo() {
// your logic
}
var foo is hoisted but it does not know the type foo yet
57 / 92
NgNinja Academy | All Rights Reserved
58 / 92
NgNinja Academy | All Rights Reserved
JavaScript Closures
Technical De nition: Closure is when a function is able to remember and access its lexical scope
even when that function is executing outside its lexical scope.
Whenever you see a function keyword within another function, the inner function has access to
variables in the outer function.
That is a closure.
Simply accessing variables outside of your immediate lexical scope creates a closure.
Below example is a closure
Because a is outside the scope of function foo
var a = 42;
Closures are just using variables that come from a higher scope
The function de ned in the closure ‘remembers’ the environment in which it was created
Closure happens when an inner function is de ned in outer function and is made accessible to be
called later.
59 / 92
NgNinja Academy | All Rights Reserved
And if you see the result - log() functions accurately logs the value of hello variable which was
originally declared in the parent function sayHello()
It means, the log() function has accurately "remembered" the value of the hello variable
This phenomenon is called closure
The value of hello variable is successfully locked into the closure of the log() function
function sayHello() {
var hello = 'Hello, world!';
return log;
}
60 / 92
NgNinja Academy | All Rights Reserved
IIFE
(function foo(){
// your code
})()
It is function expression
It is moreover a self-executing function - an IIFE
It wraps the inside members to the scope
It prevents from polluting the global scope
It is useful in closures
61 / 92
NgNinja Academy | All Rights Reserved
var foo = 20
function bar() {
foo = foo + 10
[Link](foo)
}
return bar
})()
sum() // 30
sum() // 40
sum() // 50
The interesting part is, the value of foo is enclosed inside the IIFE which is assigned to sum
And, sum is actually the function bar as you can see below
Every time you call function sum() it updates and remembers the new value of variable foo
Therefore, every call to the function displays the updated value of the foo
62 / 92
NgNinja Academy | All Rights Reserved
63 / 92
NgNinja Academy | All Rights Reserved
They all are used to attach a correct this to the function and invoke it
The di erence is the way of function invocation
bind
It returns a function
This returned function can later be called with a certain context set for calling the original function
The returned function needs to be invoked separately
var person = {
hello: function(message) {
[Link]([Link] + " says hello " + message)
}
}
var ngNinja = {
name: "NgNinja Academy"
}
64 / 92
NgNinja Academy | All Rights Reserved
call()
var person = {
hello: function(message) {
[Link]([Link] + " says hello " + message);
}
}
var ngNinja = {
name: "NgNinja Academy"
}
apply
65 / 92
NgNinja Academy | All Rights Reserved
apply also attaches this to a function and invokes the function immediately
apply is similar to call() except it takes an array of arguments instead of the comma-separated
list
var person = {
hello: function(message) {
[Link]([Link] + " says hello " + message);
}
}
var ngNinja = {
name: "NgNinja Academy"
}
66 / 92
NgNinja Academy | All Rights Reserved
Asynchronous JavaScript
Callback Function
Simple example
function getName() {
return "Sleepless Yogi";
}
function greet(callbackFn) {
// call back function is executed here
const name = callbackFn();
67 / 92
NgNinja Academy | All Rights Reserved
Asynchronous programming
- This is the type of programming where actions does not take place in a
predictable order
- Example: network calls
- When you make an HTTP call you cannot predict when the call will return
- Therefore your program needs to consider this asynchronism to out the
correct results
So, basically until we have value for the name variable we cannot print the value
We then de ne fetchAndPrintUser function to fetch the user and then print the user's name
In real world this will be a network call to some user API that queries the user database for
this information
function printUser(name) {
[Link](name)
}
function fetchAndPrintUser(printCallbackFunction) {
68 / 92
NgNinja Academy | All Rights Reserved
// Execute the function to fetch user and print the user's name
fetchAndPrintUser(printUser)
Promises
Now that you have understood what is asynchronous programming and what are callbacks
The example we saw earlier was contrived and simple - so you might not notice much di erence
BUT! in the real world applications promises simpli es the code to a great extent
TIP: When reading through this example try and compare with how we implemented the same
requirement using callbacks
As before we de ne the fetchAndPrintUser function which fetches the user details and prints
the user
But, this time instead of passing any callback function we create a new promise
69 / 92
NgNinja Academy | All Rights Reserved
What is a promise?
The Promise object itself takes a callback function with two functions as parameters
reject - function to be called if there was some error during data retrieval
So, in the example below we return Promise from the fetchAndPrintUser function
If there were any network error or some server failue - we would return error by rejecting the
promise
function fetchAndPrintUser() {
// simulate error
// when error occurs we reject the promise
if(someError) {
reject('Error ocurred!')
}
70 / 92
NgNinja Academy | All Rights Reserved
This means if the data is correctly resolved the execution goes in the then() block
Where you can do any other thing with the result data
If the promise was rejected due to some error the execution would go in the catch() block
[Link]
Let's see how to handle if you want to fetch via multiple APIs and then perform some operation on
the entire dataset
This naive way would be to declare multiple promises and then perform operations when all
promises are resolved
71 / 92
NgNinja Academy | All Rights Reserved
Like below
If you had 3 or 10 or 100 promises - can you imagine how much nesting you would have to do?
Enter [Link]!!!
Basically using this you can wait for all the promises to resolved and then only perform the next
operations
[Link]([userPromise, orderPromise])
.then((data) => {
Async-await
Similar to callback and promises, we have another paradigm for handling async programming
It is called Async-await
If you are comfortable with synchronous programming this method will be much easy to
understand
If your function is awaiting on some asynchronous data you have to de ne your function as async
And you have to use await keyword for the function call that is making the network API call
73 / 92
NgNinja Academy | All Rights Reserved
We have de ned fetchAndPrintUser function which fetches the user name and prints it
fetchUserData is the function that is making network call to the API to fetch the user data
To handle errors using async-await you have to wrap the code inside try-catch block
Like below
} catch (error) {
74 / 92
NgNinja Academy | All Rights Reserved
75 / 92
NgNinja Academy | All Rights Reserved
JavaScript Classes
class Person {
constructor(name) {
[Link] = name
}
}
Class methods
76 / 92
NgNinja Academy | All Rights Reserved
class Person {
constructor(name) {
[Link] = name
}
getName() {
return [Link]
}
}
[Link]() // John
JavaScript class is just syntactic sugar for constructor functions and prototypes
If you use typeof operator on a class it logs it as "function"
This proves that in JavaScript a class is nothing but a constructor function
example:
class Foo {}
[Link](typeof Foo); // "function"
Below example demonstrates how to achieve the same result using vanilla functions and using new
classes
You can notice how using class make your code cleaner and less verbose
77 / 92
NgNinja Academy | All Rights Reserved
Using class also makes it more intuitive and easier to understand for Developer coming from
class-based languages like Java and C++
[Link] = [Link]([Link])
[Link] = Man
[Link]([Link]) // John
[Link]([Link]) // Male
class Person {
constructor(name){
[Link] = name
}
}
78 / 92
NgNinja Academy | All Rights Reserved
[Link]([Link]) // John
[Link]([Link]) // Male
79 / 92
NgNinja Academy | All Rights Reserved
let
let keyword works very much like var keyword except it creates block-scoped variables
let keyword is an ideal candidate for loop variables, garbage collection variables
80 / 92
NgNinja Academy | All Rights Reserved
Example of let
var x declares a function scope variable which is available throughout the function
checkLetKeyword()
let x declares a block scope variable which is accessible ONLY inside the if-block
So, after the if-block the value of x is again 10
function checkLetKeyword() {
var x = 10
[Link](x) // 10
[Link](x) // 20
}
[Link](x) // 10
}
const
81 / 92
NgNinja Academy | All Rights Reserved
Tricky const
If you de ned a constant array using const you can change the elements inside it
You cannot assign a di erent array to it
But, you can add or remove elements from it
This is because const does NOT de ne a constant value. It de nes a constant reference to a value.
Example below:
MY_GRADES.push(4) // [1, 2, 3, 4]
82 / 92
NgNinja Academy | All Rights Reserved
Arrow Functions
// syntax
// example
83 / 92
NgNinja Academy | All Rights Reserved
Another example
// example
84 / 92
NgNinja Academy | All Rights Reserved
Lexical this
It means forcing the this variable to always point to the object where it is physically located within
This phenomenon is called as Lexical Scoping
Arrow function let's you achieve a lexical this via lexical scoping
Unlike a regular function, an arrow function does not bind this
It preserves the original context
It means that it uses this from the code that contains the Arrow Function
But, getName() gives an error because this is unde ned inside the function
Because in traditional function this represent the object that calls the function
And we have not assigned any object to the function invocation
85 / 92
NgNinja Academy | All Rights Reserved
var person = {
name: 'John',
printName: function(){
[Link]([Link]); // John
// John
[Link](getNameArrowFunction())
[Link]()
86 / 92
NgNinja Academy | All Rights Reserved
Destructuring Operator
It lets you unpack values from arrays, or properties from objects, into distinct variables
[Link](a) // 1
[Link](b) // 2
Your name of the variables should match the name of the properties
Order does not matter
let { b, a } = {
a: 1,
b: 2
}
[Link](a) // 1
87 / 92
NgNinja Academy | All Rights Reserved
[Link](b) // 2
Rest Operator
function log() {
log(1) // 1
log(1, 2, 3) // 1, 2, 3
It will assign all the remaining parameters to a rest-variable after those that were already assigned
numbersToLog is the rest-variable in the example below
Rest operator puts all the remaining arguments in an array and assigns it to the rest-variable
88 / 92
NgNinja Academy | All Rights Reserved
add(1, 2, 3)
Spread Operator
Example
89 / 92
NgNinja Academy | All Rights Reserved
Below example spread array1 to a comma-separated list of values into the array2
// array2 = [1, 2, 3, 4, 5]
Spread tricks
Concat array
// Without spread
var beverages = [Link](arr2)
// With spread
var beverages = [...arr1, ...arr2]
// result
// ['coffee', 'tea', 'milk', 'juice', 'smoothie']
90 / 92
NgNinja Academy | All Rights Reserved
// Without spread
var arr1Copy = [Link]()
// With spread
const arr1Copy = [...arr1]
// Without spread
// Iterate over the array add it to object as property
// If value present in the object skip it
// Else push it to another array
// With spread
const arr1Copy = [...new Set(arr1)]
// result
// ['coffee', 'tea', 'milk']
// Without spread
var bevArr = [Link]('')
// With spread
var bevArr = [myBeverage]
// result
91 / 92
NgNinja Academy | All Rights Reserved
// Without spread
var max = [Link](3, 2, 1, 5, -10)
// With spread
var myNums = [3, 2, 1, 5, -10]
var max = [Link](...myNums)
// result
// 5
92 / 92