49+ JavaScript Coding Interview Questions with answers

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

Answer: Let is a block-scoped variable declaration, while var is function-scoped. This means that variables declared with let are only accessible within the block of code in which they are defined, while variables declared with var are accessible within the entire function in which they are defined.

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

Answer: == performs type coercion, meaning it converts the operands to the same type before making the comparison. === does not perform type coercion, meaning it compares the operands without trying to convert them to the same type.

  1. What is a closure in JavaScript?

Answer: A closure is a function that has access to the variables in its lexical environment, even when the function is executed outside of that lexical environment. This allows the function to retain access to variables that would otherwise be out of scope.

  1. What is the difference between null and undefined in JavaScript?

Answer: Null is a value that represents the absence of a value or object, while undefined represents the absence of a value or object that has not been assigned.

  1. How do you create an object in JavaScript?

Answer: There are several ways to create an object in JavaScript, including:

  • Using object literal syntax: const obj = {};
  • Using the object constructor: const obj = new Object();
  • Using the object create method: const obj = Object.create(null);
  1. What is the difference between an object and an array in JavaScript?

Answer: An object is an unordered collection of key-value pairs, while an array is an ordered collection of values. Arrays have a length property and indexed elements, while objects do not.

  1. What is the difference between function declaration and function expression in JavaScript?

Answer: A function declaration is a function that is bound to a variable name at the time it is created. A function expression is a function that is created and assigned to a variable at runtime.

  1. What is the difference between a for loop and a for-in loop in JavaScript?

Answer: A for loop iterates over a block of code a specified number of times, while a for-in loop iterates over the properties of an object.

  1. What is the difference between a for-of loop and a for-in loop in JavaScript?

Answer: A for-of loop iterates over the values of an iterable object (such as an array), while a for-in loop iterates over the properties of an object.

  1. What is a callback function in JavaScript?

Answer: A callback function is a function that is passed as an argument to another function and is executed after the outer function has completed.

  1. What is the difference between synchronous and asynchronous code in JavaScript?

Answer: Synchronous code is executed in a linear, top-to-bottom fashion, while asynchronous code is executed in a non-linear manner, often with the use of callbacks or Promises.

  1. What is a Promise in JavaScript?

Answer: A Promise is an object that represents the eventual completion or failure of an asynchronous operation. It allows you to write asynchronous code in a synchronous-like style, using then() and catch() methods to handle the success or failure of the operation.

  1. What is a higher-order function in JavaScript?

Answer: A higher-order function is a function that takes one or more functions as arguments or returns a function as its output.

  1. What is a pure function in JavaScript?

Answer: A pure function is a function that always returns the same output for a given input, without causing any side effects or mutating its arguments.

  1. What is the purpose of the Array.prototype.map() method in JavaScript?

Answer: The Array.prototype.map() method creates a new array with the results of calling a provided function on every element in the calling array. It allows you to apply a transformation to each element in an array and create a new array with the transformed elements.

  1. What is the purpose of the Array.prototype.filter() method in JavaScript?

Answer: The Array.prototype.filter() method creates a new array with all elements that pass the test implemented by the provided function. It allows you to select certain elements from an array based on a condition.

  1. What is the purpose of the Array.prototype.reduce() method in JavaScript?

Answer: The Array.prototype.reduce() method applies a function to each element in the array, reducing the array to a single value. It allows you to perform an operation on all elements in an array and reduce the array to a single value.

  1. What is the purpose of the Array.prototype.sort() method in JavaScript?

Answer: The Array.prototype.sort() method sorts the elements of an array in place and returns the sorted array. It allows you to sort the elements of an array in ascending or descending order.

  1. What is the purpose of the String.prototype.split() method in JavaScript?

Answer: The String.prototype.split() method splits a string into an array of substrings based on a specified separator. It allows you to split a string into an array of substrings based on a delimiter.

  1. What is the purpose of the String.prototype.substring() method in JavaScript?

Answer: The String.prototype.substring() method extracts the characters from a string between two specified indices and returns the new substring. It allows you to extract a part of a string based on a start and end index.

  1. What is the purpose of the String.prototype.trim() method in JavaScript?

Answer: The String.prototype.trim() method removes whitespace from the beginning and end of a string and returns the modified string. It allows you to remove leading and trailing whitespace from a string.

  1. What is the purpose of the isNaN() function in JavaScript?

Answer: The isNaN() function determines whether a value is an illegal number (NaN). It allows you to check if a value is NaN.

  1. What is the purpose of the parseInt() function in JavaScript?

Answer: The parseInt() function parses a string argument and returns an integer of the specified radix (base). It allows you to convert a string representation of a number to an integer.

  1. What is the purpose of the JSON.stringify() function in JavaScript?

Answer: The JSON.stringify() function converts a JavaScript value to a JSON string. It allows you to convert a JavaScript object to a JSON string.

  1. What is the purpose of the JSON.parse() function in JavaScript?

Answer: The JSON.parse() function parses a JSON string and returns a JavaScript value. It allows you to convert a JSON string to a JavaScript object.

  1. What is the purpose of the encodeURI() function in JavaScript?

Answer: The encodeURI() function encodes a URI by replacing certain characters with their ASCII encoding. It allows you to encode a URI so it can be safely transmitted over the internet.

  1. What is the purpose of the decodeURI() function in JavaScript?

Answer: The decodeURI() function decodes an encoded URI by replacing the ASCII encoding of certain characters with their original character. It allows you to decode an encoded URI.

  1. What is the purpose of the encodeURIComponent() function in JavaScript?

Answer: The encodeURIComponent() function encodes a URI component by replacing certain characters with their ASCII encoding. It allows you to encode a URI component so it can be safely transmitted over the internet.

  1. What is the purpose of the decodeURIComponent() function in JavaScript?

Answer: The decodeURIComponent() function decodes an encoded URI component by replacing the ASCII encoding of certain characters with their original character. It allows you to decode an encoded URI component.

  1. What is the purpose of the eval() function in JavaScript?

Answer: The eval() function evaluates a string of JavaScript code and executes it. It allows you to execute JavaScript code stored in a string.

  1. What is the purpose of the parseFloat() function in JavaScript?

Answer: The parseFloat() function parses a string argument and returns a floating point number. It allows you to convert a string representation of a number to a floating point number.

  1. What is the purpose of the isFinite() function in JavaScript?

Answer: The isFinite() function determines whether a value is a finite number. It allows you to check if a value is a finite number.

  1. What is the purpose of the isInteger() function in JavaScript?

Answer: The isInteger() function determines whether a value is an integer. It allows you to check if a value is an integer.

  1. What is the purpose of the Math.round() function in JavaScript?

Answer: The Math.round() function returns the value of a number rounded to the nearest integer. It allows you to round a number to the nearest integer.

  1. What is the purpose of the Math.ceil() function in JavaScript?

Answer: The Math.ceil() function returns the smallest integer greater than or equal to a number. It allows you to round a number up to the nearest integer.

  1. What is the purpose of the Math.floor() function in JavaScript?

Answer: The Math.floor() function returns the largest integer less than or equal to a number. It allows you to round a number down to the nearest integer.

  1. What is the purpose of the Math.max() function in JavaScript?

Answer: The Math.max() function returns the maximum of a set of numbers. It allows you to find the maximum value in a set of numbers.

  1. What is the purpose of the Math.min() function in JavaScript?

Answer: The Math.min() function returns the minimum of a set of numbers. It allows you to find the minimum value in a set of numbers.

  1. What is the purpose of the Math.random() function in JavaScript?

Answer: The Math.random() function returns a random number between 0 (inclusive) and 1 (exclusive). It allows you to generate a random number.

  1. What is the purpose of the Math.abs() function in JavaScript?

Answer: The Math.abs() function returns the absolute value of a number. It allows you to get the absolute value of a number.

  1. What is the purpose of the Date() constructor in JavaScript?

Answer: The Date() constructor creates a new Date object representing a specific date and time. It allows you to create a new Date object with a specific date and time.

  1. What is the purpose of the Date.now() function in JavaScript?

Answer: The Date.now() function returns the current timestamp in milliseconds. It allows you to get the current timestamp in milliseconds.

  1. What is the purpose of the Date.getTime() function in JavaScript?

Answer: The Date.getTime() function returns the timestamp value of a Date object in milliseconds. It allows you to get the timestamp value of a Date object in milliseconds.

  1. What is the purpose of the Date.setTime() function in JavaScript?

Answer: The Date.setTime() function sets the timestamp value of a Date object in milliseconds. It allows you to set the timestamp value of a Date object in milliseconds.

  1. What is the purpose of the Date.getFullYear() function in JavaScript?

Answer: The Date.getFullYear() function returns the year value of a Date object. It allows you to get the year value of a Date object.

  1. What is the purpose of the Date.setFullYear() function in JavaScript?

Answer: The Date.setFullYear() function sets the year value of a Date object. It allows you to set the year value of a Date object.

  1. What is the purpose of the Date.getMonth() function in JavaScript?

Answer: The Date.getMonth() function returns the month value of a Date object (0-11). It allows you to get the month value of a Date object.

  1. What is the purpose of the Date.setMonth() function in JavaScript?

Answer: The Date.setMonth() function sets the month value of a Date object (0-11). It allows you to set the month value of a Date object.

  1. What is the purpose of the Date.getDate() function in JavaScript?

Answer: The Date.getDate() function returns the day of the month value of a Date object (1-31). It allows you to get the day of the month value of a Date object.

  1. What is the purpose of the Date.setDate() function in JavaScript?

Answer: The Date.setDate() function sets the day of the month value of a Date object (1-31). It allows you to set the day of the month value of a Date object.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts