Javascript most asked questions and answers with examples

codes on tilt shift lens

Top 20 JavaScript Interview Questions and Answers (2023)

  1. What is the difference between let and var in JavaScript?

Answer: var is function-scoped and let is block-scoped. This means that a variable declared with var can be accessed within the entire function, while a variable declared with let can only be accessed within the block it was declared in.

Example:

javascriptCopy codefunction example() {
  var x = 1;
  let y = 2;
  if (true) {
    var x = 3;
    let y = 4;
    console.log(x); // 3
    console.log(y); // 4
  }
  console.log(x); // 3
  console.log(y); // 2
}
  1. How do you declare a variable in JavaScript?

Answer: Variables in JavaScript can be declared using the var, let, or const keyword.

Example:

javascriptCopy codevar x = 1;
let y = 2;
const z = 3;
  1. What is closure in JavaScript?

Answer: Closure is a feature in JavaScript that allows a function to remember and access its lexical scope even when that function is invoked outside its lexical scope.

Example:

javascriptCopy codefunction makeCounter() {
  var count = 0;
  return function() {
    return count++;
  };
}

var counter = makeCounter();
console.log(counter()); // 0
console.log(counter()); // 1
console.log(counter()); // 2
  1. How do you declare an object in JavaScript?

Answer: Objects in JavaScript can be declared using object literals, or by using the Object constructor or the object initializer({})

Example:

javascriptCopy code// Using object literals
var obj = {
  key1: 'value1',
  key2: 'value2'
};

// Using the Object constructor
var obj = new Object();
obj.key1 = 'value1';
obj.key2 = 'value2';

// Using object initializer
var obj = {};
obj.key1 = 'value1';
obj.key2 = 'value2';
  1. How to check if a variable is an array in javascript

Answer: We can use the Array.isArray() method in javascript to check if a variable is an array

Example:

javascriptCopy codevar fruits = ['Apple', 'Banana'];
console.log(Array.isArray(fruits)); // true

var notArray = 'Apple, Banana';
console.log(Array.isArray(notArray)); // false
  1. How do you add an element to an array in JavaScript?

Answer: There are multiple ways to add an element to an array in JavaScript, such as using the push() method, the unshift() method, or using the square bracket notation with the assignment operator.

Example:

sqlCopy code// Using the push() method
var fruits = ['Apple', 'Banana'];
fruits.push('Orange');
console.log(fruits); // ['Apple', 'Banana', 'Orange']

// Using the unshift() method
var fruits = ['Apple', 'Banana'];
fruits.unshift('Orange');
console.log(fruits); // ['Orange', 'Apple', 'Banana']

// Using square bracket notation and the assignment operator
var fruits = ['Apple', 'Banana'];
fruits[2] = 'Orange';
console.log(fruits); // ['Apple', 'Banana', 'Orange']
  1. How do you remove an element from an array in JavaScript?

Answer: There are multiple ways to remove an element from an array in JavaScript, such as using the pop() method, the shift() method, the splice() method, or using the delete operator.

Example:

cssCopy code// Using the pop() method
var fruits = ['Apple', 'Banana', 'Orange'];
fruits.pop();
console.log(fruits); // ['Apple', 'Banana']

// Using the shift() method
var fruits = ['Apple', 'Banana', 'Orange'];
fruits.shift();
console.log(fruits); // ['Banana', 'Orange']

// Using the splice() method
var fruits = ['Apple', 'Banana', 'Orange'];
fruits.splice(1, 1);
console.log(fruits); // ['Apple', 'Orange']

// Using the delete operator
var fruits = ['Apple', 'Banana', 'Orange'];
delete fruits[1];
console.log(fruits); // ['Apple', undefined, 'Orange']
  1. How do you loop through an array in JavaScript?

Answer: There are multiple ways to loop through an array in JavaScript, such as using a for loop, a forEach() method, or a for...of loop.

Example:

javascriptCopy code// Using a for loop
var fruits = ['Apple', 'Banana', 'Orange'];
for (var i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

// Using the forEach() method
var fruits = ['Apple', 'Banana', 'Orange'];
fruits.forEach(function(fruit) {
  console.log(fruit);
});

// Using a for...of loop
var fruits = ['Apple', 'Banana', 'Orange'];
for (var fruit of fruits) {
  console.log(fruit);
}
  1. How do you check if a variable is defined in JavaScript?

Answer: To check if a variable is defined in JavaScript, you can use the typeof operator or the === undefined comparison.

Example:

javascriptCopy codevar x;
if (typeof x === 'undefined') {
  console.log('x is not defined');
  1. How do you create an object in JavaScript?

Answer: There are multiple ways to create an object in JavaScript, such as using object literal notation, the object constructor, or the object.create() method.

Example:

javascriptCopy code// Using object literal notation
var person = {
  name: 'John Doe',
  age: 30
};
console.log(person); // { name: 'John Doe', age: 30 }

// Using the object constructor
var person = new Object();
person.name = 'John Doe';
person.age = 30;
console.log(person); // { name: 'John Doe', age: 30 }

// Using the object.create() method
var person = Object.create(null);
person.name = 'John Doe';
person.age = 30;
console.log(person); // { name: 'John Doe', age: 30 }
  1. How do you access the properties of an object in JavaScript?

Answer: To access the properties of an object in JavaScript, you can use the dot notation or the square bracket notation.

Example:

javascriptCopy codevar person = {
  name: 'John Doe',
  age: 30
};
console.log(person.name); // 'John Doe'
console.log(person['age']); // 30

var prop = 'name';
console.log(person[prop]); // 'John Doe'
  1. How do you create a function in JavaScript?

Answer: There are multiple ways to create a function in JavaScript, such as using function declaration, function expression, or the arrow function syntax.

Example:

javascriptCopy code// Using function declaration
function add(a, b) {
  return a + b;
}
console.log(add(1, 2)); // 3

// Using function expression
var add = function(a, b) {
  return a + b;
};
console.log(add(1, 2)); // 3

// Using the arrow function syntax
var add = (a, b) => {
  return a + b;
};
console.log(add(1, 2)); // 3
  1. How do you add an element to an array in JavaScript?

Answer: There are several methods to add an element to an array in JavaScript, such as using the push() method, the unshift() method, or the splice() method.

Example:

sqlCopy codevar fruits = ['apple', 'banana'];

// Using the push() method
fruits.push('orange');
console.log(fruits); // ['apple', 'banana', 'orange']

// Using the unshift() method
fruits.unshift('strawberry');
console.log(fruits); // ['strawberry', 'apple', 'banana', 'orange']

// Using the splice() method
fruits.splice(1, 0, 'mango');
console.log(fruits); // ['strawberry', 'mango', 'apple', 'banana', 'orange']
  1. How do you remove an element from an array in JavaScript?

Answer: There are several methods to remove an element from an array in JavaScript, such as using the pop() method, the shift() method, or the splice() method.

Example:

scssCopy codevar fruits = ['apple', 'banana', 'orange'];

// Using the pop() method
fruits.pop();
console.log(fruits); // ['apple', 'banana']

// Using the shift() method
fruits.shift();
console.log(fruits); // ['banana']

// Using the splice() method
fruits.splice(0, 1);
console.log(fruits); // []
  1. How do you loop through an array in JavaScript?

Answer: There are several ways to loop through an array in JavaScript, such as using a for loop, a forEach() method, or a for-of loop.

Example:

javascriptCopy codevar fruits = ['apple', 'banana', 'orange'];

// Using a for loop
for (var i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

// Output:
// apple
// banana
// orange

// Using the forEach() method
fruits.forEach(function(fruit) {
  console.log(fruit);
});

// Output:
// apple
// banana
// orange

// Using a for-of loop
for (var fruit of fruits) {
  console.log(fruit);
}

// Output:
// apple
// banana
// orange
  1. What is a callback function in JavaScript?

Answer: A callback function in JavaScript is a function that is passed as an argument to another function and is executed after the outer function has completed. Callback functions are often used in asynchronous programming to perform an action after an event has occurred or a task has been completed.

Example:

sqlCopy codefunction add(a, b, callback) {
  var result = a + b;
  callback(result);
}

add(1, 2, function(result) {
  console.log(result); // 3
});

In this example, the add() function takes three arguments: two numbers to add and a callback function. The callback function is executed after the add() function has completed, and the result of the addition is passed as an argument.

  1. What is the difference between == and === in JavaScript?

Answer: The == operator in JavaScript compares the values of two variables for equality, while the === operator compares both the values and the types of the variables.

Example:

javascriptCopy codeconsole.log(1 == '1'); // true
console.log(1 === '1'); // false

In this example, the == operator compares the values of the number 1 and the string ‘1’ and returns true because they are equal. However, the === operator compares both the value and the type of the variables and returns false because one is a number and the other is a string.

  1. What is hoisting in JavaScript?

Answer: Hoisting in JavaScript is a behavior where variables and function declarations are moved to the top of the scope in which they are defined. In other words, variables and functions can be used before they are declared in the code.

Example:

javascriptCopy codeconsole.log(x); // undefined
var x = 5;
console.log(x); // 5

In this example, the first console.log statement logs undefined, even though the variable x has not been declared yet. This is because the variable is hoisted to the top of the scope and is initialized with the value undefined.

  1. What is closure in JavaScript?

Answer: A closure in JavaScript is a function that has access to the variables in the scope in which it was created, even after the outer function has returned. Closures can be used to preserve state and create private variables.

Example:

javascriptCopy codefunction makeCounter() {
  var count = 0;
  return function() {
    return count++;
  }
}

var counter = makeCounter();
console.log(counter()); // 0
console.log(counter()); // 1
console.log(counter()); // 2

In this example, the makeCounter() function returns an inner function that has access to the count variable in the outer function’s scope. The inner function can increment and return the count variable, even after the makeCounter() function has returned.

  1. What is the difference between let and var in JavaScript?

Answer: The main difference between let and var in JavaScript is their scope. Variables declared with var are function scoped, which means they are only accessible within the function in which they are declared. Variables declared with let are block scoped, which means they are only accessible within the block in which they are declared, including any nested blocks.

Example:

javascriptCopy codeif (true) {
  var x = 5;
  let y = 10;
}
console.log(x); // 5
console.log(

Leave a Reply

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