Chapter 2 Flashcards

1
Q

Difference between let and const?

A

Let and const are ways to create variables. (ES6)

Let - variable values
Const - constant value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Jsbin.com

A

It is used to test js on a website

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Arrow functions

A

Const func = () => {

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the use arrow function?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to create a Modular code?

How to split code into multiple files?

Guve example

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are classes in javascript?

How is it constructor called?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Initalize properties and methods in javascript.

A

We can also use direct invocation withiut constructor and use latest functiondefinition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are rest and spread operators?

A

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()
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is destructuring?

A

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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Primitive types and reference types

How to deep copy a reference type?

A

Primitive types - number string
Reference type - arrays, object

To copy an object one can use spread operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Map function

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

find()

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

findIndex()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

filter()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

reduce()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

slice()

A

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

var animals = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘elephant’];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
17
Q

splice()

A

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

var months = [‘Jan’, ‘March’, ‘April’, ‘June’];
months.splice(1, 0, ‘Feb’);
// inserts at index 1
console.log(months);
// expected output: Array [‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘June’]

months.splice(4, 1, ‘May’);
// replaces 1 element at index 4
console.log(months);
// expected output: Array [‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘May’]