Scope determines the availability or accessibility or visibility of variables, objects, and functions.
- Global Scope
- Local Scope
- Block Scope
- Function Scope
The difference between let and var is of Scoping.
- let gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var.
- var is rather a keyword which defines a variable globally regardless of block scope.
The both are very similar when used like the following, outside a function block.
Window Object: However, global variables defined with let will not be added as properties on the global window object.
Block Scope: let has block level scope.
Redeclaration: let and const variables cannot be re-declared while var variable can be re-declared in the same scope.
A function expression can be stored in a variable and the variable can be used as a function
There are many benefits of using function expressions
- It can be used as Closure
- It can be passed as arguments to other Function
- It cab be used as Immediately Invoked Function Expressions (IIFE)
Function Expressions can not be Hoisted.
- It is a design pattern which is also known as a Self-Executing Anonymous Function
- variables and function declarations are moved to the top of their scope before code execution.
1. Anonymous Function - This type of function has no name.
2. Regular Named Function - This function is named properly and specifically.
3. Arrow Function - Arrow function (kind of anonymous function) is part of ES6.
Arrow functions is best suited with anything that requires this to be bound to the context, and not the function itself.
- Shorter Syntax
- No binding of this
Unlike a regular function, an arrow function does not bind this. Instead, this is bound lexically (i.e. this keeps it's meaning from its original context).
An arrow function does not have its own this. this value of the enclosing lexical scope is used by arrow functions. So if this is not present in current scope, an arrow function ends up looking into lexical scope.
You may also like - React.js Interview Questions
Object is an unordered collection of data in the form of key:value pairs.
- Object Literals
- Object Constructor
- Object.create Method
- using Function
- using Class
Object.create() method creates a new object, using an existing object as the prototype of the newly created object.
Primitives data types like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference.
The comparison by reference basically checks to see if the objects given refer to the same location in memory.
- It has access to it's own scope i.e. variables defined between its curly brackets.
- It has access to the outer function's variables.
- It has access to the global variables.
Reason: It will reference the last value stored in i, which was 5.
There are two solution to resolve this problem
Closure It creates a unique scope for each iteration, storing each unique value of the variable within it's scope.
Reason: let has block level scope so variable i is only seen in the for loop's block scope.
A callback function is a function passed into another function as an argument(let’s call this other function "otherFunction"), and the callback function gets executed inside the otherFunction after the function has finished execution.
Callbacks are a way to make sure certain code doesn't execute until other code has already finished execution.
You may also like - Node.js Interview Questions and Answers
The property [[Prototype]] is internal and hidden, but there are many ways to set it. via __proto__or Object.getPrototypeOf
- Object.create(proto[, descriptors]) – creates an empty object with given proto as [[Prototype]] and optional property descriptors.
- Object.getPrototypeOf(obj) – returns the [[Prototype]] of obj.
- Object.setPrototypeOf(obj, proto) – sets the [[Prototype]] of obj to proto.
Classical Inheritance: A constructor function instantiates an instance via the new keyword. This new instance inherits properties from a parent class.
Prototypal Inheritance: An instance is created by cloning an existing object that serves as a prototype. This instance—often instantiated using a factory function or "Object.create()"—can benefit from selective inheritance from many different objects.
A promise is an object which can be returned synchronously from an asynchronous function.
Promise overcome the problem of callback hell.
- Fulfilled: onFulfilled() will be called (e.g. resolve() was called).
- Rejected: onRejected() will be called (e.g. reject() was called).
- Pending: initial state, neither fulfilled nor rejected.
The apply() method calls a function with a given this value, and arguments provided as an array.
Syntax: fun.apply(thisArg, [argsArray])
The call() method calls a function with a given this value and arguments provided individually
Syntax: fun.call(thisArg[, arg1[, arg2[, ...]]])
A useful mnemonic is "A for array and C for comma."
bind allows you to set the this value now while allowing you to execute the function in the future, because it returns a new function object.
Syntax: fun.bind(thisArg[, arg1[, arg2[, ...]]])
The spread operator (…) is a new operator introduced in ES6. It allows the expansion of an iterable (e.g., Array) into its constituent elements.
- Using ES6 - Spread & Set Operator
- Using jQuery each function
Set was introduced in ES6: They can't have duplicate Items.
After we have done that, we simply convert the Set back to an array by using the spread operator.
Design patterns are advanced object-oriented solutions to commonly occurring software problems.
As the name suggests, these patterns are for handling object creational mechanisms.
- Constructor pattern
- Factory pattern
- Prototype pattern
- Singleton pattern
This pattern helps to obtain new functionalities without tampering with the existing ones.
These patterns are concerned with improving communication between dissimilar objects
- Iterator pattern
- Observer pattern
- Template pattern
- Strategy pattern
- State pattern
- Command pattern
If you will define two or more functions with the same name, the last one will override the previously defined functions and when a call will be made to the function, the last defined function will get executed.
Yes, it's true. since null is a primitive datatype still typeof null return object.
- Tag Type
- Actual Value
tag refering for object and null was same, that's why typeof null return object.