Chapter 2 Flashcards
Difference between let and const?
Let and const are ways to create variables. (ES6)
Let - variable values
Const - constant value
Jsbin.com
It is used to test js on a website
Arrow functions
Const func = () => {
}
What is the use arrow function?
Solves issues with this keyword in js. When this is written in arrow function it will always keep it’s context and not change is suprisingly at run time
Makes code shorter Eg. const mul = num => num*2;
How to create a Modular code?
How to split code into multiple files?
Guve example
Using import and export.
Eg. const person = { name: ‘Max’ }
Export default person
File 2 Export const clean = () => {...} Export const baseData = => {...}
Import person from ‘./person.js’
Import {baseData as baseData, clean} from ‘./utility.js’
Import * as bundled from ‘./utility.js’
The. Use bundled.clean etc.
What are classes in javascript?
How is it constructor called?
Class Person {
Constructor () {
This.Name: ‘ma’
}
PrintName = () => {..}
}
Class is instantiated with new keyword.
Class can be extended using extends keyword
class Person extends Master
Constructor() method is available to initialize properties of class
Extended class must have a super(); keyword at the beginning if the constructor.
Initalize properties and methods in javascript.
We can also use direct invocation withiut constructor and use latest functiondefinition
What are rest and spread operators?
Its just …
Spread
Used to split up array or object properties
Const newArray = […oldArray, 1, 2]
Const newObject = {…oldObject, newProp: 5}
Rest Used to merge a list of function arguments into an array Function sortArgs(...args) { Return args.sort() }
What is destructuring?
Easily extract array elements or object properties and store them in variables
Const numbers = [1,2,3]
[a,,b] = numbers
{name} = { name: ‘Max’, age: 29}
Primitive types and reference types
How to deep copy a reference type?
Primitive types - number string
Reference type - arrays, object
To copy an object one can use spread operator
Map function
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
var array1 = [1, 4, 9, 16];
// pass a function to map const map1 = array1.map(x => x * 2);
console.log(map1); // expected output: Array [2, 8, 18, 32]
find()
he find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned. var array1 = [5, 12, 8, 130, 44];
var found = array1.find(function(element) { return element > 10; });
console.log(found); // expected output: 12
findIndex()
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
var words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];
const result = words.filter(word => word.length > 6);
console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
reduce()
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); // expected output: 10
// 5 + 1 + 2 + 3 + 4 console.log(array1.reduce(reducer, 5)); // expected output: 15