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 employee = { | |
name: 'John', | |
age: 30, | |
salary: 1000 | |
} | |
const {name, ...others} = employee; | |
// ...others means that all the other | |
// properties of the object will be | |
// stored in the others 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
let employee = { | |
name: 'John', | |
age: 30, | |
salary: 1000 | |
} | |
let {salary, name} = employee; | |
// this is an example of destructuring | |
// assignment. We call this as destructuring | |
// because we are destructuring the object |
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 arr = [1,2,3,4]; | |
// console.log(arr[0]); // 1 | |
// console.log(arr[1]); // 2 | |
// console.log(arr[2]); // 3 | |
const [f, s] = arr; | |
// the above concept is called | |
// destructuring because we are | |
// destructuring the array into |
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 arr1 = [ | |
1,2,3 | |
] | |
let arr2 = [ | |
4,5,6 | |
] | |
let negativeNumbers = [ | |
-1,-2,-3 | |
] |
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 student = { | |
name: 'Mike', | |
age: 23, | |
city: 'Paris' | |
} | |
let studentParentDetails = { | |
fatherName: 'John', | |
motherName: 'Jane' | |
} | |
// To merge two objects, we can use the spread operator |
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
// ES6 using arrow functions | |
let myFunctionVersion2 = (a, b) => 100 | |
/** | |
* Another way of writing the same function is | |
* let myFunctionVersion2 = (a, b) => { | |
* return 100 | |
* } | |
* | |
* In ES5: | |
* function myFunctionVersion2(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
// ES6 | |
// ES6 means ECMAScript 6, which is the latest version of JavaScript. | |
// ECMAScript is the organization that sets the standards for JavaScript. | |
// The version before ES6 was ES5. | |
// ES5 | |
let myFunction = function (a, b) { | |
return a * b; | |
} | |
console.log(myFunction(2, 3)); // 6 | |
// ES6 using arrow functions |
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
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | |
// let arr = [3, 56, -1, 0, -45, 899, 78] | |
let arr = ["oranges", "apples", "aaples", "bananas"]; | |
let sorted = arr.sort(); | |
// sort function sorts the array in lexographical order | |
// which means it will sort the array as if it is a string | |
// so the output will be [-1, -45, 0, 3, 56, 78, 899] | |
// because -1 comes before -45, 0 comes before 3, etc. |
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; | |
} | |
function subtract(a, b) { | |
return a - b; | |
} | |
function sin(theta) { | |
return Math.sin(theta); |
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; | |
} | |
function printSum() { | |
return sum; | |
} // printSum is now a higher-order function | |
// because it returns a function as a result | |
// printSum() will return a function sum and then you |