Skip to content

Instantly share code, notes, and snippets.

View suhailgupta03's full-sized avatar
:octocat:

Suhail Gupta suhailgupta03

:octocat:
View GitHub Profile
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
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
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
let arr1 = [
1,2,3
]
let arr2 = [
4,5,6
]
let negativeNumbers = [
-1,-2,-3
]
let student = {
name: 'Mike',
age: 23,
city: 'Paris'
}
let studentParentDetails = {
fatherName: 'John',
motherName: 'Jane'
}
// To merge two objects, we can use the spread operator
// 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) {
// 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
// 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.
function sum(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function sin(theta) {
return Math.sin(theta);
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