JavaScript Flashcards

1
Q

What are types of promise?

A

Promise.all waits for all promises to be resolved or for any to be rejected.

Promise.allSettled waits for all promises to settle (either resolved or rejected) and returns an array of objects describing the outcome of each promise.

Promise.any waits for any of the promises to be resolved and returns its value. If all promises are rejected, it rejects with an AggregateError.

Promise.race returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects.

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

Explain the different types of variable declarations in TypeScript (var, let, const)

A

var declares a variable with function scope, let declares a block-scoped variable, and const declares a block-scoped constant that cannot be reassigned. let and const are recommended over var to avoid scope-related issues.

var can be redeclared, but not forlet and const

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

What is this?

A

This is a keyword that refers to the context in which a function is called. Its value depends on how and where the function is invoked, and it can be tricky because it behaves differently in various situations.

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

Types of using this?

A

Global Context

console.log(this); // window (in a browser)

Regular Function Call
~~~
function regularFunction() {
console.log(this);
}

regularFunction(); // window (in non-strict mode) or undefined (in strict mode)
~~~

Method Call
~~~
const obj = {
method: function() {
console.log(this);
}
};

obj.method(); // obj
~~~

Arrow Function
~~~
const obj = {
method: function() {
const arrowFunc = () => {
console.log(this);
};
arrowFunc();
}
};

obj.method(); // obj
~~~

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