Some things about JavaScript

Short introduction

1. What are the possible ways to create objects in JavaScript?

Object constructor:

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

Function constructor:

Create any function and apply the new operator to create object instances,

Output:

Function constructor with prototype:

This is similar to function constructor but it uses prototype for their properties and methods,

Output:

ES6 Class syntax:

ES6 introduces class feature to create the objects

Output:

Singleton pattern:

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.

Output:

2. What is the difference between Call, Apply and Bind?

Call:

The call() method invokes a function with a given this value and arguments provided one by one

Output:

Apply:

Invokes the function with a given this value and allows you to pass in arguments as an array

Output:

bind:

returns a new function, allowing you to pass any number of arguments

Output:

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

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

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

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.

Slice:

  1. Doesn’t modify the original array(immutable)
  2. Returns the subset of original array
  3. Used to pick the elements from array

Splice:

  1. Modifies the original array(mutable)
  2. Returns the deleted elements as array
  3. Used to insert or delete elements to/from array

7. What is the difference between == and === operators

  1. Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  2. Two numbers are strictly equal when they are numerically equal. i.e, having the same number value. There are two special cases in this,
  3. NaN is not equal to anything, including NaN.
  4. Positive and negative zeros are equal to one another.
  5. Two Boolean operands are strictly equal if both are true or both are false.
  6. Two objects are strictly equal if they refer to the same object.
  7. 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

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?

I am Aiman. I am a web developer. I can learn anything at any time. For that, I take the help of Google. My core skill is based on JavaScript .