The simplest way to create an empty object is using the Object constructor. Currently, this approach is not recommended.
Object’s create method:
The create method of Object creates a new object by passing the prototype object as a parameter
Object literal syntax:
The object literal syntax is equivalent to create method when it passes null as parameter
Create any function and apply the new operator to create object instances,
Function constructor with prototype:
This is similar to function constructor but it uses prototype for their properties and methods,
ES6 Class syntax:
ES6 introduces class feature to create the objects
A Singleton is an object which can only be instantiated one time. Repeated calls to its constructor return the same instance and this way one can ensure that they don’t accidentally create multiple instances.
2. What is the difference between Call, Apply and Bind?
The difference between Call, Apply and Bind can be explained with below examples,
The call() method invokes a function with a given
this value and arguments provided one by one
Invokes the function with a given
this value and allows you to pass in arguments as an array
returns a new function, allowing you to pass any number of arguments
Call and apply are pretty interchangeable. Both execute the current function immediately. You need to decide whether it’s easier to send in an array or a comma separated list of arguments. You can remember by treating Call is for comma (separated list) and Apply is for Array.
Whereas Bind creates a new function that will have
this set to the first parameter passed to bind().
3. What is JSON and its common operations
Douglas Crockford It is useful when you want to transmit data across a network, and it is basically just a text file with an extension of .JSON.
Parsing: Converting a string to a native object
Stringification: converting a native object to a string so it can be transmitted across the network
4. What is the purpose of the array slice method
The slice() method returns the selected elements in an array as a new array object. It selects the elements starting at the given start argument, and ends at the given optional end argument without including the last element. If you omit the second argument then it selects till the end.
Some of the examples of this method are,
Note: Slice method won’t mutate the original array but it returns the subset as a new array.
5. What is the purpose of the array splice method
The splice() method is used either adds/removes items to/from an array, and then returns the removed item. The first argument specifies the array position for insertion or deletion whereas the option second argument indicates the number of elements to be deleted. Each additional argument is added to the array.
Some examples of this method are,
Note: Splice method modifies the original array and returns the deleted array.
6. What is the difference between slice and splice.
Some of the major difference in a tabular form
- Doesn’t modify the original array(immutable)
- Returns the subset of original array
- Used to pick the elements from array
- Modifies the original array(mutable)
- Returns the deleted elements as array
- Used to insert or delete elements to/from array
7. What is the difference between == and === operators
- Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
- Two numbers are strictly equal when they are numerically equal. i.e, having the same number value. There are two special cases in this,
- NaN is not equal to anything, including NaN.
- Positive and negative zeros are equal to one another.
- Two Boolean operands are strictly equal if both are true or both are false.
- Two objects are strictly equal if they refer to the same object.
- Null and Undefined types are not equal with ===, but equal with ==. i.e, null===undefined → false but null==undefined → true
Some of the example which covers the above cases,
8. What are lambda or arrow functions
An arrow function is a shorter syntax for a function expression and does not have its own this, arguments, super, or new.target. These functions are best suited for non-method functions, and they cannot be used as constructors.
9. What is IIFE(Immediately Invoked Function Expression)
The primary reason to use an IIFE is to obtain data privacy because any variables declared within the IIFE cannot be accessed by the outside world. i.e, If you try to access variables with IIFE then it throws an error as below,
10. What is a higher order function?
Higher-order function is a function that accepts another function as an argument or returns a function as a return value or both