Some things about JavaScript

Ahad Hossain Aiman
6 min readMay 5, 2021

Short introduction

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

There are many ways to create an object in JavaScript as below:

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?

The difference between Call, Apply and Bind can be explained with below examples,

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

JSON is a text-based data format following JavaScript object syntax, which was popularized by.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

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

JavaScript provides both strict(===, !==) and type-converting(==, !=) equality comparison. The strict operators take the type of variable in consideration, while non-strict operators make type correction/conversion based upon values of variables. The strict operators follow the below conditions for different types,

  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

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)

IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The signature of it would be as below,

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

--

--

Ahad Hossain Aiman

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 .