This file contains hidden or 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
// closure is a function having access to the parent | |
// scope | |
function greet() { | |
var message = "Good evening"; | |
function sayHi() { | |
console.log(message); | |
} // Here closure is created, | |
// sayHi function has access to message variable |
This file contains hidden or 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
// setInterval(function() { | |
// console.log("Playing MP4"); | |
// },500); // continuous loop | |
// // every 500 ms, it will print "Playing MP4" | |
setTimeout(function() { | |
console.log("Downloaded MP4"); | |
}, 3000); // 3s | |
This file contains hidden or 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
setInterval(function() { | |
console.log("Playing MP4"); | |
}, 1000); | |
// the first argument is the callback | |
// function | |
// the reason we call it as a callback | |
// function is because it is called back | |
// after a certain time interval |
This file contains hidden or 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
console.log(x); | |
var x = 1; | |
// Here x is undefined | |
// because JavaScript hoists declarations | |
// which means that the declaration is | |
// automatically moved to the top of | |
// the current scope | |
greet(); | |
// greet function is hoisted |
This file contains hidden or 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
function calculator(operation, a, b) { | |
if(operation == "add") { | |
return a + b; | |
}else if(operation == "subtract") { | |
return a - b; | |
}else if(operation == "multiply") { | |
return a * b; | |
}else if(operation == "divide") { | |
return a / b; | |
}else { |
This file contains hidden or 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
let numbers = [100, 0, 99, -1, 999, 4] | |
let sorted = numbers.sort((a, b) => a - b); // by default sort sorts in ascending order | |
// when a - b > 0, this means a > b, so a should be after b | |
// which means that larger number a should be after smaller number b | |
console.log(sorted) | |
let sortedDesc = numbers.sort((a, b) => b - a); // sort in descending order | |
// when b - a > 0, this means b > a, so b should be after a | |
// which means that larger number b should be after smaller number a |
This file contains hidden or 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
let i = 0; | |
function greet() { | |
i++; | |
console.log("Hello, World! ", i); | |
if( i >= 500) { | |
return; | |
} // it is very important to have a base case | |
// if you do not have a base case, you will | |
// have an infinite loop |
This file contains hidden or 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
// IIFE | |
// Immediately Invoked Function Expression | |
(function() { | |
var x = ".." | |
var y = "...." | |
var z = "......" | |
console.log(x) | |
console.log(y) | |
console.log(z) |
This file contains hidden or 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
// function sum(a, b) { | |
// return a + b; | |
// } | |
// const x = function sum(a, b) { | |
// return a + b; | |
// } // function expression | |
// // because it is assigned to a variable | |
const x = function (a, b) { |
This file contains hidden or 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
// function sum(a, b) { | |
// return a + b; | |
// } | |
// let v = sum(99, 1); | |
// console.log(v); | |
var x = function sum(a, b) { | |
return a + b; | |
} // x is a function | |
// the reason x is a function is because it is a |