Ticker

6/recent/ticker-posts

Javascript interview question and their answer in 2025

              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 language used in client side that allows us (developers) to make web pages interactive.***

 A scripting language is a kind of programming language that is used to automate the execution of operations in a runtime environment.

JavaScript is an object-based scripting language, meaning it uses objects to represent data and functionality,
JavaScript is also cross-platform, which means it can be run on any device that has a web browser,
JavaScript follow synchronous pattern
JavaScript code is a single thread, which means that code can only do one task at a time.
Dynamic Typing ****
JavaScript is dynamically typed, which means: we don’t need to mention the type of a variable.
A variable can hold any type of value, and it can change later.

let x = 10;           // x is a number
x = "Hello";         // now x is a string
x = [1, 2, 3];      // now x is an array
x = { name: "JS" } // now x is an object

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 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?

 All three are used to set the value of 'this' inside a function.

Call method:

'call' invoke a function immediately and set the value of 'this' inside that function. we can also pass arguments to the function 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:

apply is similar to call, but instead of passing arguments one by one, we pass them as an array.
  It also invokes the function immediately and sets the value of 'this' .  


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:

 bind doesn’t calls the function immediately. Instead, it creates a new function with a fixed 'this' value

that we can call later.


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 (JavaScript Object Notation) is preferred over other data formats
 
Lightweight and Simple: JSON is a compact, text-based format that's easy to read and write for both humans and machines.
It uses key-value pairs, making it simpler than XML, which has verbose tags.

Native JavaScript Support: JSON is a subset of JavaScript, so it’s directly parsed into JavaScript objects
using JSON.parse().
This eliminates extra parsing steps needed for XML, making it faster and more efficient.

Smaller Data Size: JSON results in smaller payloads compared to XML, reducing network transfer time
( in AJAX requests,) which improves performance.

Wide Compatibility: JSON is supported by (virtually) all modern APIs and programming languages,
making it a universal choice for data exchange in AJAX calls.

Easier to Work With: JSON’s structure (objects, arrays, strings, numbers) aligns naturally with JavaScript,
allowing straightforward manipulation compared to XML’s complex DOM parsing.

 

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 array.Modifies 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