DS Modern JS Flashcards
How to destruct an array?
//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 to destruct an object?
//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);
What is a spread operator?
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.
What is a rest pattern?
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);
What is short circuiting?
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.
What is nullish coalescing operator?
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 to loop arrays using for-of?
//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
}
What is optional chaining?
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 to loop over an object?
//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
}
What is a Map?
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())
When to use set vs array?
When we need a list of data we use.
Use set when we need unique data and High performance.
When to use map vs object?
We can use both. Map was introduced in ES6.
How to read one character of a string?
use [], example, name[1];
How to get a substring of a string?
use slice(startIndex, endIndex). this will not change the original string. Strings are 0 based.
How to change the string to lower case?
use toLowercase()