JavaScript Flashcards
What are types of promise?
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.
Explain the different types of variable declarations in TypeScript (var
, let
, const
)
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
What is this
?
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.
Types of using this
?
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
~~~