DS Modern JS Flashcards

1
Q

How to destruct an array?

A

//Array Destructing

const arr = [1, 2, 3, 4, 5];

//1st way
const [a, b, c, d, f] = arr;

console.log(a, b, c, d, f);

//2nd Way
console.log(arr[1]);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to destruct an object?

A

//Object destructing

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue",
};

//1st Way

let { firstName, age } = person;

console.log(firstName, age);

//provide our own Name

let { firstName: FN, age: aa } = person;

console.log(FN, aa);

//Mutating the variables

let firstName2, age2;
const person2 = {
  firstName2: "John",
  lastName2: "Doe",
  age2: 50,
  eyeColor2: "blue",
};

({ firstName2, age2 } = person2);

console.log(firstName2, age2);

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

What is a spread operator?

A

Spread Operator is used to expand the array to all its elements.

Usage:

let arr1 = new Array(1, 2, 3);
console.log(...arr1);

To join two arrays:
let arr2 = new Array(…arr1, 5, 6);
console.log(…arr2);

To pass multiple values to a function:

let arr3 = new Array();
arr3[0] = 1;
arr3[1] = 2;
let speadDemoFunc = function (...arr5) {
  console.log(...arr5);
};

speadDemoFunc(arr3);

More Info:
Spread syntax (...) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a rest pattern?

A

the (…) can be used left side of the assignment operator. Rest pattern is just opposite of spread when used left side of the assignment operator. When used, the data will be stored packed inside an array.

/**************

What is a rest operator?

**************/

let […restdemo] = [1, 2, 3, 4, 5, 6, 7, 7];

console.log(restdemo);

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

What is short circuiting?

A

console.log(3 || ‘bharath’)
The short circuiting will return the first truthy value in an expression. If no truthy value found, then it will return the last falsy value. This is with || operator.

But, if we use it with && operator, then it returns the first falsy value.

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

What is nullish coalescing operator?

A

It is same as || short circuiting. But, it works for null and undefined values(NOT 0 or ‘’).

/**************
What is nullish coalescing operator?
**
**************/

let objDemo = {
  name: "Bharath",
};

console.log(objDemo?.name);

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

How to loop arrays using for-of?

A

//For-of loop example

const arrForOf = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

for (const item of arrForOf) {
console.log(item);
}

//if we need index, then we need to

for (const item of arrForOf.entries()) {
console.log(item[0]); //index
console.log(item[1]); //value
}

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

What is optional chaining?

A
If the object/value is nullish, then it will return undefined. This will avoid to many if conditions.
const greeting = object?.deepProp?.deeperProp?.greet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to loop over an object?

A

//looping over an Object

//to get the keys from the object
const keys = Object.keys(person);

for (const item of keys) {
console.log(item);
}

//To read the values,

const values = Object.values(person);

for (const item of values) {
console.log(item);
}

//To read both at a time
const entri = Object.entries(person);

for (const item of entri) {
console.log(item[0]); // key
console.log(item[1]); // values
}

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

What is a Map?

A

Map is a DS that holds key and value.

To set a map:
let mapOb = new Map();
mapOb.set('key', 'value');
this returns the map it self. hence we can chain

To get a map,
mapOb.get(‘Key’);

To delete a map,
mapOb.delete(‘key’);

we can also create map as:
let mapOb = new Map([
[‘key’,’value’]
]);

To iterate,
for([key,value] of mapOb)

we can convert object to map,
 let mapOb = new Map(object.entries())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When to use set vs array?

A

When we need a list of data we use.

Use set when we need unique data and High performance.

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

When to use map vs object?

A

We can use both. Map was introduced in ES6.

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

How to read one character of a string?

A

use [], example, name[1];

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

How to get a substring of a string?

A

use slice(startIndex, endIndex). this will not change the original string. Strings are 0 based.

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

How to change the string to lower case?

A

use toLowercase()

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

How to replace a character in a string?

A

.replace(‘toBereplaced’, ‘newChar’); returns a repalced string.

17
Q

How to check if a substring is available?

A

includes() or startsWIth()

18
Q

How to divide a string?

A

.split(‘divider’)

19
Q

How to join a string?

A

.join(‘joiner’); exmaple. [‘Bharath’, ‘Kumar’].join(‘ ‘);

20
Q

How to add a padding?

A

padStart(multipler, ‘char’); simialrly padEnd()