JavaScript Interview Questions & Answers

What are the possible ways to create objects in JavaScript?

answer:-There are many ways to create objects in javascript as mentioned below:

  1. Object literal syntax:The object literal syntax (or object initializer), is a comma-separated set of name-value pairs wrapped in curly braces.

var object = {
     name: "Sudheer",
     age: 34
};

Object literal property values can be of any data type, including array, function, and nested object.

Object constructor:

The simplest way to create an empty object is using the Object constructor. Currently this approach is not recommended.

var object = new Object();

The Object() is a built-in constructor function so “new” keyword is not required. The above code snippet can be re-written as:

var object = Object();

Object’s create method:

The create method of Object is used to create a new object by passing the specificied prototype object and properties as arguments, i.e., this pattern is helpful to create new objects based on existing objects. The second argument is optional and it is used to create properties on a newly created object.

var object = Object.create(null);

Function constructor:

Imagine you want to create a function that represents a blueprint for creating objects. This function typically contains instructions on how to set up and initialize the object.

function Person(name) {
  this.name = name;
  this.age = 21;
}
var object = new Person("Sudheer");

What is a prototype chain:-

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

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
  console.log(
    greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
  );
}

invite.call(employee1, "Hello", "How are you?"); // Hello John Rodson, How are you?
invite.call(employee2, "Hello", "How are you?"); // Hello Jimmy Baily, How are you?

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

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
  console.log(
    greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
  );
}

invite.apply(employee1, ["Hello", "How are you?"]); // Hello John Rodson, How are you?
invite.apply(employee2, ["Hello", "How are you?"]); // Hello Jimmy Baily, How are you?

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

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
  console.log(
    greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
  );
}

var inviteEmployee1 = invite.bind(employee1);
var inviteEmployee2 = invite.bind(employee2);
inviteEmployee1("Hello", "How are you?"); // Hello John Rodson, How are you?
inviteEmployee2("Hello", "How are you?"); // Hello Jimmy Baily, How are you?

What is the purpose of the array slice method?

  • Start at a Certain Point:
    • You can tell the tool where to begin picking elements by giving it a starting point. It’s like saying, “Start picking from here.”
  • Optional Endpoint:
    • You can also tell the tool where to stop picking by giving it an optional ending point. It’s important to note that the element at the ending point won’t be included in what’s picked. It’s like saying, “Stop picking just before reaching this point.”
  • Result is a New List:
    • The tool doesn’t modify the original list; instead, it creates a completely new list with the picked elements. Think of it like making a copy of the selected elements.
  • If No Endpoint Provided:
    • If you don’t mention where to stop, the tool will keep picking elements until the end of the list.

Some of the examples of this method are,

let arrayIntegers = [1, 2, 3, 4, 5];
let arrayIntegers1 = arrayIntegers.slice(0, 2); // returns [1,2]
let arrayIntegers2 = arrayIntegers.slice(2, 3); // returns [3]
let arrayIntegers3 = arrayIntegers.slice(4); //returns [5]

What is the purpose of the array splice method?

The splice() method adds/removes items to/from an array, and then returns the removed item. The first argument specifies the array position/index for insertion or deletion whereas the optional second argument indicates the number of elements to be deleted. Each additional argument is added to the array.

Some of the examples of this method are:

let arrayIntegersOriginal1 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal2 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal3 = [1, 2, 3, 4, 5];

let arrayIntegers1 = arrayIntegersOriginal1.splice(0, 2); // returns [1, 2]; original array: [3, 4, 5]
let arrayIntegers2 = arrayIntegersOriginal2.splice(3); // returns [4, 5]; original array: [1, 2, 3]
let arrayIntegers3 = arrayIntegersOriginal3.splice(3, 1, "a", "b", "c"); //returns [4]; original array: [1, 2, 3, "a", "b", "c", 5]

What is the difference between slice and splice?

SliceSplice
Doesn’t modify the original array(immutable)Modifies the original array(mutable)
Returns the subset of original arrayReturns the deleted elements as array
Used to pick the elements from arrayUsed to insert/delete elements to/from array

thank you………

Leave a Reply

Your email address will not be published. Required fields are marked *