This repository contains 85 JavaScript interview questions and answers tailored for the job market in Bangladesh. These questions are designed to help you prepare for technical interviews and improve your understanding of JavaScript concepts. The questions range from basic to advanced topics, ensuring comprehensive preparation.
Table of Contents
- Basic JavaScript Questions
- Intermediate JavaScript Questions
- Advanced JavaScript Questions
- DOM Manipulation Questions
- Asynchronous JavaScript Questions
- ES6+ Features Questions
- JavaScript Coding Challenges
Basic JavaScript Questions
-
What is JavaScript?
- JavaScript is a high-level, interpreted programming language used to make web pages interactive. It is a core technology of the web alongside HTML and CSS.
-
What are the data types in JavaScript?
- JavaScript has 7 primitive data types:
string,number,boolean,null,undefined,symbol, andbigint. It also has one non-primitive data type:object.
- JavaScript has 7 primitive data types:
-
What is the difference between
let,const, andvar?-
varis function-scoped and can be redeclared and updated. -
letis block-scoped and can be updated but not redeclared. -
constis block-scoped and cannot be updated or redeclared.
-
-
What is the difference between
==and===?-
==checks for equality after type coercion. -
===checks for strict equality without type coercion.
-
-
What is
NaNin JavaScript?-
NaNstands for "Not-a-Number" and is a value that represents an invalid number.
-
-
What is the use of
typeofoperator?- The
typeofoperator is used to determine the type of a variable or expression.
- The
-
What is the difference between
nullandundefined?-
nullis an intentional absence of any value, whileundefinedmeans a variable has been declared but not assigned a value.
-
-
What is hoisting in JavaScript?
- Hoisting is a mechanism where variable and function declarations are moved to the top of their scope during the compilation phase.
-
What is the scope in JavaScript?
- Scope refers to the accessibility or visibility of variables, functions, and objects in some particular part of your code during runtime.
-
What is the difference between
function declarationandfunction expression?- A function declaration is hoisted, while a function expression is not.
Intermediate JavaScript Questions
-
What is a closure in JavaScript?
- A closure is a function that has access to its outer function's scope, even after the outer function has returned.
-
What is the
thiskeyword in JavaScript?- The
thiskeyword refers to the object that the function is a property of. Its value depends on how the function is called.
- The
-
What is event delegation?
- Event delegation is a technique where you add a single event listener to a parent element to handle events for all its child elements.
-
What is the difference between
nullandundefined?-
nullis an intentional absence of any value, whileundefinedmeans a variable has been declared but not assigned a value.
-
-
What is hoisting in JavaScript?
- Hoisting is a mechanism where variable and function declarations are moved to the top of their scope during the compilation phase.
-
What is the difference between
call,apply, andbind?-
callandapplyare used to invoke a function with a specificthisvalue and arguments. -
bindcreates a new function with a specificthisvalue but does not invoke it immediately.
-
-
What are promises in JavaScript?
- Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value.
-
What is async/await in JavaScript?
-
async/awaitis syntactic sugar for working with promises, making asynchronous code look and behave like synchronous code.
-
-
What is the event loop in JavaScript?
- The event loop is a mechanism that allows JavaScript to perform non-blocking I/O operations by offloading operations to the system kernel.
-
What is the difference between
setTimeoutandsetInterval?-
setTimeoutexecutes a function once after a specified delay, whilesetIntervalexecutes a function repeatedly at specified intervals.
-
Advanced JavaScript Questions
-
What is the prototype chain in JavaScript?
- Every object in JavaScript has a prototype, and the prototype chain allows objects to inherit properties and methods from their prototypes.
-
What is the difference between
call,apply, andbind?-
callandapplyare used to invoke a function with a specificthisvalue and arguments. -
bindcreates a new function with a specificthisvalue but does not invoke it immediately.
-
-
What are promises in JavaScript?
- Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value.
-
What is async/await in JavaScript?
-
async/awaitis syntactic sugar for working with promises, making asynchronous code look and behave like synchronous code.
-
-
What is the event loop in JavaScript?
- The event loop is a mechanism that allows JavaScript to perform non-blocking I/O operations by offloading operations to the system kernel.
-
What is a generator function in JavaScript?
- A generator function is a special type of function that can be paused and resumed, allowing you to produce a sequence of values over time.
-
What is the difference between
MapandObjectin JavaScript?-
Mapallows keys of any type, maintains insertion order, and has asizeproperty, whileObjectonly allows string or symbol keys and does not guarantee order.
-
-
What is the difference between
WeakMapandMap?-
WeakMapallows garbage collection of its keys if there are no other references to them, whileMapdoes not.
-
-
What is the difference between
WeakSetandSet?-
WeakSetallows garbage collection of its values if there are no other references to them, whileSetdoes not.
-
-
What is the purpose of
Symbolin JavaScript?-
Symbolis a primitive data type used to create unique identifiers for object properties.
-
DOM Manipulation Questions
-
What is the DOM?
- The DOM (Document Object Model) is a programming interface for HTML and XML documents. It represents the structure of a document as a tree of objects.
-
How do you select an element by its ID?
- Use
document.getElementById('elementId').
- Use
-
How do you add a class to an element?
- Use
element.classList.add('className').
- Use
-
What is the difference between
innerHTMLandtextContent?-
innerHTMLreturns the HTML content of an element, whiletextContentreturns only the text content.
-
-
How do you create a new element and append it to the DOM?
- Use
document.createElement('tagName')andparentElement.appendChild(newElement).
- Use
-
What is the difference between
appendChildandinsertBefore?-
appendChildadds a node as the last child of a parent element, whileinsertBeforeadds a node before a specified reference node.
-
-
How do you remove an element from the DOM?
- Use
parentElement.removeChild(childElement).
- Use
-
What is the difference between
querySelectorandgetElementById?-
querySelectorcan select any element using a CSS selector, whilegetElementByIdonly selects elements by their ID.
-
-
How do you handle events in JavaScript?
- Use
addEventListenerto attach an event handler to an element.
- Use
-
What is event bubbling and event capturing?
- Event bubbling is the propagation of an event from the target element up to the root, while event capturing is the propagation from the root down to the target element.
Asynchronous JavaScript Questions
-
What is a callback function?
- A callback function is a function passed as an argument to another function and is executed after some operation is completed.
-
What is the difference between synchronous and asynchronous code?
- Synchronous code executes line by line, blocking further execution until the current operation is complete. Asynchronous code allows other operations to run while waiting for the current operation to complete.
-
What is the purpose of
setTimeout?-
setTimeoutis used to execute a function after a specified delay (in milliseconds).
-
-
What is a race condition in JavaScript?
- A race condition occurs when the output of a program depends on the timing of uncontrollable events, such as the order of asynchronous operations.
-
What is the difference between
Promise.allandPromise.race?-
Promise.allwaits for all promises to resolve, whilePromise.raceresolves or rejects as soon as one of the promises resolves or rejects.
-
-
What is the purpose of
async/await?-
async/awaitsimplifies working with promises by allowing you to write asynchronous code in a synchronous style.
-
-
What is a microtask in JavaScript?
- A microtask is a task that is executed after the current task and before the next task in the event loop. Examples include
Promisecallbacks.
- A microtask is a task that is executed after the current task and before the next task in the event loop. Examples include
-
What is the difference between
setImmediateandsetTimeout?-
setImmediateexecutes a function immediately after the current event loop cycle, whilesetTimeoutexecutes a function after a specified delay.
-
-
What is the purpose of
process.nextTickin Node.js?-
process.nextTickschedules a callback to be executed immediately after the current operation completes, before any I/O events.
-
-
What is the difference between
Promise.resolveandPromise.reject?-
Promise.resolvecreates a resolved promise, whilePromise.rejectcreates a rejected promise.
-
ES6+ Features Questions
-
What are arrow functions?
- Arrow functions are a shorter syntax for writing functions in JavaScript. They do not have their own
thisand are always anonymous.
- Arrow functions are a shorter syntax for writing functions in JavaScript. They do not have their own
-
What are template literals?
- Template literals are string literals that allow embedded expressions, using backticks (
`) and${}syntax.
- Template literals are string literals that allow embedded expressions, using backticks (
-
What is destructuring in JavaScript?
- Destructuring is a syntax that allows you to unpack values from arrays or properties from objects into distinct variables.
-
What are default parameters?
- Default parameters allow you to set default values for function parameters if no value or
undefinedis passed.
- Default parameters allow you to set default values for function parameters if no value or
-
What are JavaScript modules?
- JavaScript modules are reusable pieces of code that can be exported from one program and imported into another.
-
What is the difference between
letandconst?-
letallows reassignment, whileconstdoes not.
-
-
What is the spread operator in JavaScript?
- The spread operator (
...) allows an iterable to be expanded in places where multiple arguments or elements are expected.
- The spread operator (
-
What is the rest parameter in JavaScript?
- The rest parameter (
...) allows you to represent an indefinite number of arguments as an array.
- The rest parameter (
-
What is the purpose of
Symbolin JavaScript?-
Symbolis a primitive data type used to create unique identifiers for object properties.
-
-
What is the difference between
MapandObjectin JavaScript?-
Mapallows keys of any type, maintains insertion order, and has asizeproperty, whileObjectonly allows string or symbol keys and does not guarantee order.
-
JavaScript Coding Challenges
-
Write a function to reverse a string.
function reverseString(str) { return str.split('').reverse().join(''); } -
Write a function to check if a string is a palindrome.
function isPalindrome(str) { return str === str.split('').reverse().join(''); } -
Write a function to find the largest number in an array.
function findLargestNumber(arr) { return Math.max(...arr); } -
Write a function to remove duplicates from an array.
function removeDuplicates(arr) { return [...new Set(arr)]; } -
Write a function to flatten a nested array.
function flattenArray(arr) { return arr.flat(Infinity); } -
Write a function to find the factorial of a number.
function factorial(n) { if (n === 0 || n === 1) return 1; return n * factorial(n - 1); } -
Write a function to check if a number is prime.
function isPrime(n) { if (n <= 1) return false; for (let i = 2; i <= Math.sqrt(n); i++) { if (n % i === 0) return false; } return true; } -
Write a function to find the Fibonacci sequence up to a given number.
function fibonacci(n) { const sequence = [0, 1]; for (let i = 2; i <= n; i++) { sequence.push(sequence[i - 1] + sequence[i - 2]); } return sequence; } -
Write a function to sort an array of numbers in ascending order.
function sortArray(arr) { return arr.sort((a, b) => a - b); } -
Write a function to find the sum of all elements in an array.
function sumArray(arr) { return arr.reduce((sum, num) => sum + num, 0); } -
Write a function to find the average of all elements in an array.
function averageArray(arr) { return arr.reduce((sum, num) => sum + num, 0) / arr.length; } -
Write a function to find the second largest number in an array.
function secondLargest(arr) { const sortedArr = arr.sort((a, b) => b - a); return sortedArr[1]; } -
Write a function to count the number of vowels in a string.
function countVowels(str) { const vowels = ['a', 'e', 'i', 'o', 'u']; return str.split('').filter(char => vowels.includes(char)).length; } -
Write a function to capitalize the first letter of each word in a string.
function capitalizeWords(str) { return str.split(' ').map(word => word[0].toUpperCase() + word.slice(1)).join(' '); } -
Write a function to find the longest word in a string.
function longestWord(str) { return str.split(' ').reduce((longest, word) => word.length > longest.length ? word : longest, ''); } -
Write a function to check if two strings are anagrams.
function areAnagrams(str1, str2) { return str1.split('').sort().join('') === str2.split('').sort().join(''); } -
Write a function to find the intersection of two arrays.
function intersection(arr1, arr2) { return arr1.filter(value => arr2.includes(value)); } -
Write a function to find the union of two arrays.
function union(arr1, arr2) { return [...new Set([...arr1, ...arr2])]; } -
Write a function to find the difference between two arrays.
function difference(arr1, arr2) { return arr1.filter(value => !arr2.includes(value)); } -
Write a function to find the index of a specific element in an array.
function findIndex(arr, element) { return arr.indexOf(element); } -
Write a function to remove falsy values from an array.
function removeFalsy(arr) { return arr.filter(Boolean); } -
Write a function to check if an array is sorted.
function isSorted(arr) { return arr.every((value, index, array) => index === 0 || value >= array[index - 1]); } -
Write a function to rotate an array to the right by a given number of steps.
function rotateArray(arr, steps) { for (let i = 0; i < steps; i++) { arr.unshift(arr.pop()); } return arr; } -
Write a function to find the first non-repeating character in a string.
function firstNonRepeatingChar(str) { const charCount = {}; for (const char of str) { charCount[char] = (charCount[char] || 0) + 1; } for (const char of str) { if (charCount[char] === 1) return char; } return null; } Write a function to find the most frequent element in an array.
javascript
function mostFrequent(arr) {
const frequencyMap = {};
let maxCount = 0;
let mostFrequentElement = null;
for (const num of arr) {
frequencyMap[num] = (frequencyMap[num] || 0) + 1;
if (frequencyMap[num] > maxCount) {
maxCount = frequencyMap[num];
most
Top comments (0)