JavaScript Flashcards
What is a Promise in JavaScript?
A promise is a special JavaScript object that represents the eventual completion or failure of an asynchronous operation. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.it is one of three states. pending, fulfilled, or rejected.
What are the primitive data types?
A primitive is a data type that isn’t composed of other data types and unlike reference data types like objects, their values are immutable means that their values can’t be modified. it can be changed though. They are:
* Boolean
* Undefined
* Null
* Number
* String
Describe this keyword in JS ?
The this keyword refers to the object that is currently executing the code. as functions are also objects, when used inside a function, the value of this will change depending on how that function is defined, how it is invoked, and the default execution context.
1- In Implicit Binding: When we call a function as a method of the object this keyword refers to the calling object.
2- In Explicit Binding: When we explicitly bind this keyword using the call(), bind(), or apply() method then this keyword default reference is changed to the object called using the above-specified methods.
3- In Default Binding: When this keyword is used in global scope this is set to the window object.
4- The arrow functions do not get the “this” keyword.
instead, if you use the “this” keyword in an arrow function, it will simply be the “this” keyword of the surrounding function,so of the parent function. and in technical terms,
this is called the “lexical this keyword”.
5- If the function is called as an event listener, then the this keyword will point to the DOM element that the function is attached to.
What is hoisting in JavaScript?
Hoisting refers to the process in which the interpreter moves the declaration of functions, classes, and variables to the top of their scope before their line of execution.
it only happens when the functions are declared using function or async function keywords.
In this case, the behavior would be described as Being able to use a variable’s value in its scope before the line is declared. (it’s called value hoisting).
And in the case of variables declared with var, the behavior would be described as Being able to reference a variable in its scope before the line it is declared (called Declaration hoisting)
What are the higher-order functions in JS?
Functions in JS are first-class objects.
1) they have built-in properties and methods like name property and toString() method.
2) New properties and methods can be assigned to them.
3) They can be passed as arguments to other functions and returned from other functions.
So higher-order functions are functions that accept functions as parameters or return a function.
for example, the reduce,forEach,filter, and map methods, all of them accept functions as parameters so they are higher-order functions.
What is a callback function in JS?
The callback function is a function passed as an argument to another function. This function can then be invoked during the execution of that higher order function (that it is an argument of).
Since, in JavaScript, functions are objects, functions can be passed as arguments.
What is closure in JS?
A closure is a feature that gives a function the ability to reference the variables in its outer scope from its inner scope. the closure preserves the outer scope inside its inner scope.
so A closure function(which is a function declared inside another function) has access to three scopes.
1) Its own scope(inner scope)
2) The scope of the outer function in which it was declared.
3) the global scope.
How can OOP be implemented in JS?