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 instancesfunction 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 objectsclass 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:
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 array | Modifies the original array |
Returns a subset of the original array | Returns the deleted elements as an array |
Used to pick elements from an array | Used 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.
0 Comments