DEV Community

Cover image for JavaScript Interview Questions and Answers for the Job Market in Bangladesh
Md Nazmus Sakib
Md Nazmus Sakib

Posted on

JavaScript Interview Questions and Answers for the Job Market in Bangladesh

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

  1. Basic JavaScript Questions
  2. Intermediate JavaScript Questions
  3. Advanced JavaScript Questions
  4. DOM Manipulation Questions
  5. Asynchronous JavaScript Questions
  6. ES6+ Features Questions
  7. JavaScript Coding Challenges

Basic JavaScript Questions

  1. 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.
  2. What are the data types in JavaScript?

    • JavaScript has 7 primitive data types: string, number, boolean, null, undefined, symbol, and bigint. It also has one non-primitive data type: object.
  3. What is the difference between let, const, and var?

    • var is function-scoped and can be redeclared and updated.
    • let is block-scoped and can be updated but not redeclared.
    • const is block-scoped and cannot be updated or redeclared.
  4. What is the difference between == and ===?

    • == checks for equality after type coercion.
    • === checks for strict equality without type coercion.
  5. What is NaN in JavaScript?

    • NaN stands for "Not-a-Number" and is a value that represents an invalid number.
  6. What is the use of typeof operator?

    • The typeof operator is used to determine the type of a variable or expression.
  7. What is the difference between null and undefined?

    • null is an intentional absence of any value, while undefined means a variable has been declared but not assigned a value.
  8. 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.
  9. 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.
  10. What is the difference between function declaration and function expression?

    • A function declaration is hoisted, while a function expression is not.

Intermediate JavaScript Questions

  1. 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.
  2. What is the this keyword in JavaScript?

    • The this keyword refers to the object that the function is a property of. Its value depends on how the function is called.
  3. 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.
  4. What is the difference between null and undefined?

    • null is an intentional absence of any value, while undefined means a variable has been declared but not assigned a value.
  5. 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.
  6. What is the difference between call, apply, and bind?

    • call and apply are used to invoke a function with a specific this value and arguments.
    • bind creates a new function with a specific this value but does not invoke it immediately.
  7. What are promises in JavaScript?

    • Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value.
  8. What is async/await in JavaScript?

    • async/await is syntactic sugar for working with promises, making asynchronous code look and behave like synchronous code.
  9. 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.
  10. What is the difference between setTimeout and setInterval?

    • setTimeout executes a function once after a specified delay, while setInterval executes a function repeatedly at specified intervals.

Advanced JavaScript Questions

  1. 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.
  2. What is the difference between call, apply, and bind?

    • call and apply are used to invoke a function with a specific this value and arguments.
    • bind creates a new function with a specific this value but does not invoke it immediately.
  3. What are promises in JavaScript?

    • Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value.
  4. What is async/await in JavaScript?

    • async/await is syntactic sugar for working with promises, making asynchronous code look and behave like synchronous code.
  5. 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.
  6. 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.
  7. What is the difference between Map and Object in JavaScript?

    • Map allows keys of any type, maintains insertion order, and has a size property, while Object only allows string or symbol keys and does not guarantee order.
  8. What is the difference between WeakMap and Map?

    • WeakMap allows garbage collection of its keys if there are no other references to them, while Map does not.
  9. What is the difference between WeakSet and Set?

    • WeakSet allows garbage collection of its values if there are no other references to them, while Set does not.
  10. What is the purpose of Symbol in JavaScript?

    • Symbol is a primitive data type used to create unique identifiers for object properties.

DOM Manipulation Questions

  1. 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.
  2. How do you select an element by its ID?

    • Use document.getElementById('elementId').
  3. How do you add a class to an element?

    • Use element.classList.add('className').
  4. What is the difference between innerHTML and textContent?

    • innerHTML returns the HTML content of an element, while textContent returns only the text content.
  5. How do you create a new element and append it to the DOM?

    • Use document.createElement('tagName') and parentElement.appendChild(newElement).
  6. What is the difference between appendChild and insertBefore?

    • appendChild adds a node as the last child of a parent element, while insertBefore adds a node before a specified reference node.
  7. How do you remove an element from the DOM?

    • Use parentElement.removeChild(childElement).
  8. What is the difference between querySelector and getElementById?

    • querySelector can select any element using a CSS selector, while getElementById only selects elements by their ID.
  9. How do you handle events in JavaScript?

    • Use addEventListener to attach an event handler to an element.
  10. 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

  1. 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.
  2. 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.
  3. What is the purpose of setTimeout?

    • setTimeout is used to execute a function after a specified delay (in milliseconds).
  4. 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.
  5. What is the difference between Promise.all and Promise.race?

    • Promise.all waits for all promises to resolve, while Promise.race resolves or rejects as soon as one of the promises resolves or rejects.
  6. What is the purpose of async/await?

    • async/await simplifies working with promises by allowing you to write asynchronous code in a synchronous style.
  7. 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 Promise callbacks.
  8. What is the difference between setImmediate and setTimeout?

    • setImmediate executes a function immediately after the current event loop cycle, while setTimeout executes a function after a specified delay.
  9. What is the purpose of process.nextTick in Node.js?

    • process.nextTick schedules a callback to be executed immediately after the current operation completes, before any I/O events.
  10. What is the difference between Promise.resolve and Promise.reject?

    • Promise.resolve creates a resolved promise, while Promise.reject creates a rejected promise.

ES6+ Features Questions

  1. What are arrow functions?

    • Arrow functions are a shorter syntax for writing functions in JavaScript. They do not have their own this and are always anonymous.
  2. What are template literals?

    • Template literals are string literals that allow embedded expressions, using backticks (`) and ${} syntax.
  3. What is destructuring in JavaScript?

    • Destructuring is a syntax that allows you to unpack values from arrays or properties from objects into distinct variables.
  4. What are default parameters?

    • Default parameters allow you to set default values for function parameters if no value or undefined is passed.
  5. What are JavaScript modules?

    • JavaScript modules are reusable pieces of code that can be exported from one program and imported into another.
  6. What is the difference between let and const?

    • let allows reassignment, while const does not.
  7. 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.
  8. What is the rest parameter in JavaScript?

    • The rest parameter (...) allows you to represent an indefinite number of arguments as an array.
  9. What is the purpose of Symbol in JavaScript?

    • Symbol is a primitive data type used to create unique identifiers for object properties.
  10. What is the difference between Map and Object in JavaScript?

    • Map allows keys of any type, maintains insertion order, and has a size property, while Object only allows string or symbol keys and does not guarantee order.

JavaScript Coding Challenges

  1. Write a function to reverse a string.

    function reverseString(str) {
        return str.split('').reverse().join('');
    }
    
  2. Write a function to check if a string is a palindrome.

    function isPalindrome(str) {
        return str === str.split('').reverse().join('');
    }
    
  3. Write a function to find the largest number in an array.

    function findLargestNumber(arr) {
        return Math.max(...arr);
    }
    
  4. Write a function to remove duplicates from an array.

    function removeDuplicates(arr) {
        return [...new Set(arr)];
    }
    
  5. Write a function to flatten a nested array.

    function flattenArray(arr) {
        return arr.flat(Infinity);
    }
    
  6. 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);
    }
    
  7. 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;
    }
    
  8. 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;
    }
    
  9. Write a function to sort an array of numbers in ascending order.

    function sortArray(arr) {
        return arr.sort((a, b) => a - b);
    }
    
  10. Write a function to find the sum of all elements in an array.

    function sumArray(arr) {
        return arr.reduce((sum, num) => sum + num, 0);
    }
    
  11. 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;
    }
    
  12. 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];
    }
    
  13. 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;
    }
    
  14. 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(' ');
    }
    
  15. 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, '');
    }
    
  16. Write a function to check if two strings are anagrams.

    function areAnagrams(str1, str2) {
        return str1.split('').sort().join('') === str2.split('').sort().join('');
    }
    
  17. Write a function to find the intersection of two arrays.

    function intersection(arr1, arr2) {
        return arr1.filter(value => arr2.includes(value));
    }
    
  18. Write a function to find the union of two arrays.

    function union(arr1, arr2) {
        return [...new Set([...arr1, ...arr2])];
    }
    
  19. Write a function to find the difference between two arrays.

    function difference(arr1, arr2) {
        return arr1.filter(value => !arr2.includes(value));
    }
    
  20. Write a function to find the index of a specific element in an array.

    function findIndex(arr, element) {
        return arr.indexOf(element);
    }
    
  21. Write a function to remove falsy values from an array.

    function removeFalsy(arr) {
        return arr.filter(Boolean);
    }
    
  22. 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]);
    }
    
  23. 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;
    }
    
  24. 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;
    }
    
  25. 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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)