Header Ads Widget

Responsive Advertisement

Javascript interview question and their answer in 2024

              Most asking  interview questions for javascript




 Hello Guys! Today We prepared a huge list of Javascript Interview Questions and their answers.

Q:) What is JS (Javascript)

JavaScript is a scripting or programming language that allows us to implement complex features on web pages.

JavaScript is an object-oriented programming language designed to make web development easier and more attractive. In most cases, JavaScript is used to create responsive, interactive elements for web pages, enhancing the user experience.


Q:) What are the possible ways to create objects in JavaScript

There are many ways to create objects in javascript as below

 i. 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(); 

    

ii. Object's create method: 

The create method of Object creates a new object by passing the prototype object as a parameter

var object = Object.create(null);


iii. Object literal syntax: 

The object literal syntax is equivalent to creating a method when it passes null as a parameter

var object = {};


iv. Function constructor:

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

function Person(name){

 var object = {};

object.name=name;
object.age=21;
return object;
}
var object = new Person("Sudheer");


v. ES6 Class syntax:

 ES6 introduces a class feature to create objects

 class Person {
constructor(name) {
this.name = name;
   }
}
var object = new Person("Sudheer"); 


 Q:) What is a prototype chain?

Prototype chaining is used to build new types of objects based on existing ones. It is similar to inheritance in a class-based language.


Q:) What is the difference between Call, Apply, and Bind?

 Call method:

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


var employee1 = {firstName: 'tony', lastName: 'stark'};
var employee2 = {firstName: 'Jim', lastName: 'Bai'};

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

invite.call(employee1, 'Hello', 'How are you?'); // Hello tony stark, How are you? invite.call(employee2, 'Hello', 'How are you?'); // Hello Jim Bai, How are you?

Apply method:

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

var employee1 = {firstName: 'Jon', lastName: 'son'};
var employee2 = {firstName: 'Jim', lastName: 'Bail'};

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

invite.apply(employee1, ['Hello', 'How are you?']); // Hello Jon son, How are you? invite.apply(employee2, ['Hello', 'How are you?']); // Hello Jim Bai, How are you?


bind method:

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


var employee1 = {firstName: 'Jon', lastName: 'son'};
var employee2 = {firstName: 'Jim', lastName: 'Bai'};
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 Jon son, How are you? inviteEmployee2('Hello', 'How are you?'); // Hello Jim Bai, How are you?


Q:)What is JSON and its common operations?

 JSON is a text-based data format following JavaScript object syntax,  It is useful when we want to transmit data across a network and it is basically just a text file with an extension of .json, and a MIME type of application/json. 


Q:) 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.

example

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]

Note: Slice method won't mutate the original array but it returns the subset as a new array

Q:) What is the purpose of the array splice method?

The splice() method in JavaScript is a powerful tool for adding, removing, or replacing elements in an array. It can perform these actions directly on the original array, and it also returns an array containing the removed elements.

Here's how it works:

The first argument specifies the index at which to start the operation (adding/removing elements).

The second argument indicates the number of elements to remove from the array, starting at the specified index. If this argument is omitted, all elements from the specified index to the end of the array are removed.

Any additional arguments provided to splice() are added to the array at the specified index.

For example:


let arrayIntegersOriginal = [1, 2, 3, 4, 5];

let arrayIntegers1 = arrayIntegersOriginal.splice(0, 2); // Removes elements starting at index 0, and removes 2 elements, returns [1, 2]
// Now, arrayIntegersOriginal becomes [3, 4, 5]

let arrayIntegers2 = arrayIntegersOriginal.splice(3); // Removes elements starting at index 3 (inclusive), returns [4, 5]
// Now, arrayIntegersOriginal becomes [3]

let arrayIntegers3 = arrayIntegersOriginal.splice(3, 1, "a", "b", "c"); // Removes 1 element starting at index 3 and replaces it with "a", "b", "c", returns [4]
// Now, arrayIntegersOriginal becomes [3, "a", "b", "c"]

Key points to remember:

splice() modifies the original array directly.
It can remove elements, add elements, or both.
The removed elements are returned as a new array.
Additional arguments provided to splice() are added to the array at the specified index.


Q:) What is the difference between slice and splice

slice()splice()
Doesn't modify the original arrayModifies the original array
Returns a subset of the original arrayReturns the deleted elements as an array
Used to pick elements from an arrayUsed to insert, delete, or replace elements in an array

Explanation:

slice():

slice() doesn't change the original array; it returns a new array containing the selected elements.
It's used when you want to extract a portion of an array without altering the original array.

For example, if you have an array and you want to get a portion of it, you can use slice().

splice():

splice() directly modifies the original array by adding, removing, or replacing elements.

It's used when you need to make changes to the array itself, like adding new elements, removing existing ones, or replacing elements.

Additionally, splice() returns an array containing the elements that were removed.

let array = [1, 2, 3, 4, 5];

let slicedArray = array.slice(0, 2); // Returns [1, 2], array remains unchanged

let splicedArray = array.splice(2, 1, 'a', 'b'); // Removes 1 element starting from index 2, adds 'a' and 'b' at that position, returns [3]
// Now, array becomes [1, 2, 'a', 'b', 4, 5]




Post a Comment

0 Comments