Top 50 JavaScript Coding Interview Questions & Answers (2025 Edition)

JavaScript interviews focus heavily on logic, string manipulation, array operations, recursion, and problem-solving.
Here are the top 50 JavaScript coding interview questions with solutions + outputs.


1. Reverse a String

function reverseString(str) {
  return str.split("").reverse().join("");
}

console.log(reverseString("hello"));

Output:

olleh


2. Check Palindrome

function isPalindrome(str) {
  return str === str.split("").reverse().join("");
}

console.log(isPalindrome("madonna"));
console.log(isPalindrome("madam"));

Output:

false
true


3. Reverse an Integer

function reverseInt(num) {
  return parseInt(num.toString().split("").reverse().join(""));
}

console.log(reverseInt(12345));

Output:

54321


4. Factorial of a Number

function factorial(n) {
  return n === 0 ? 1 : n * factorial(n - 1);
}

console.log(factorial(5));

Output:

120


5. Largest Number in Array

console.log(Math.max(...[3, 10, 6, 2]));

Output:

10


6. Smallest Number in Array

console.log(Math.min(...[3, 10, 6, 2]));

Output:

2


7. Sum of Array Numbers

function sumArray(arr) {
  return arr.reduce((a, b) => a + b, 0);
}

console.log(sumArray([5, 5, 5]));

Output:

15


8. Remove Duplicates

console.log([...new Set([1, 2, 2, 3, 3, 4])]);

Output:

[1, 2, 3, 4]


9. Anagram Check

console.log(isAnagram("listen", "silent"));

function isAnagram(a, b) {
  return a.split("").sort().join("") === b.split("").sort().join("");
}

Output:

true


10. Character Count

console.log(charCount("hello"));

function charCount(str) {
  return str.split("").reduce((acc, ch) => {
    acc[ch] = (acc[ch] || 0) + 1;
    return acc;
  }, {});
}

Output:

{ h: 1, e: 1, l: 2, o: 1 }


11. Fibonacci Sequence (Iterative)

function fibonacci(n) {
  const arr = [0, 1];
  for (let i = 2; i <= n; i++) {
    arr[i] = arr[i - 1] + arr[i - 2];
  }
  return arr;
}

console.log(fibonacci(7));

Output:

[0, 1, 1, 2, 3, 5, 8, 13]


12. Check Prime Number

function isPrime(n) {
  if (n < 2) return false;
  for (let i = 2; i <= Math.sqrt(n); i++) {
    if (n % i === 0) return false;
  }
  return true;
}

console.log(isPrime(11));
console.log(isPrime(20));

Output:

true
false


13. Find Second Largest Number

function secondLargest(arr) {
  const unique = [...new Set(arr)];
  unique.sort((a, b) => b - a);
  return unique[1];
}

console.log(secondLargest([10, 20, 30, 30, 5]));

Output:

20


14. Find Missing Number

function missingNumber(arr) {
  const n = arr.length + 1;
  const sum = (n * (n + 1)) / 2;
  return sum - arr.reduce((a, b) => a + b, 0);
}

console.log(missingNumber([1,2,3,5]));

Output:

4


15. Flatten Array

console.log([1, [2, [3, 4]]].flat(Infinity));

Output:

[1, 2, 3, 4]


16. Check if Arrays are Equal

function arraysEqual(a, b) {
  return JSON.stringify(a) === JSON.stringify(b);
}

console.log(arraysEqual([1,2,3], [1,2,3]));
console.log(arraysEqual([1,2,3], [3,2,1]));

Output:

true
false


17. Array Intersection

console.log([1,2,3,4].filter(x => [3,4,5].includes(x)));

Output:

[3, 4]


18. Count Vowels

function countVowels(str) {
  return str.match(/[aeiou]/gi)?.length || 0;
}

console.log(countVowels("javascript"));

Output:

3


19. Capitalize First Letter of Each Word

console.log("hello world".replace(/\b\w/g, c => c.toUpperCase()));

Output:

Hello World


20. Random Number (Range)

function random(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(random(10, 20));

Output Example:

14


21. Validate Email

console.log(/^\S+@\S+\.\S+$/.test("abc@gmail.com"));
console.log(/^\S+@\S+\.\S+$/.test("hello@com"));

Output:

true
false


22. Check Numeric String

console.log(/^\d+$/.test("12345"));
console.log(/^\d+$/.test("12a45"));

Output:

true
false


23. Convert to Title Case

function titleCase(str) {
  return str.toLowerCase().replace(/\b\w/g, c => c.toUpperCase());
}

console.log(titleCase("welcome to javascript"));

Output:

Welcome To Javascript


24. GCD (Greatest Common Divisor)

function gcd(a, b) {
  return b === 0 ? a : gcd(b, a % b);
}

console.log(gcd(20, 8));

Output:

4


25. LCM

function gcd(a, b) {
  return b === 0 ? a : gcd(b, a % b);
}
function lcm(a, b) {
  return (a * b) / gcd(a, b);
}

console.log(lcm(5, 10));

Output:

10


26. String Rotation Check

console.log(isRotation("abcde", "cdeab"));

function isRotation(a, b) {
  return a.length === b.length && (a + a).includes(b);
}

Output:

true


27. Longest Word

function longestWord(str) {
  return str.split(" ").reduce((a, b) => b.length > a.length ? b : a);
}

console.log(longestWord("JavaScript coding interview questions"));

Output:

interview


28. Remove All Spaces

console.log("a b  c d".replace(/\s+/g, ""));

Output:

abcd


29. Word Count

console.log("  JavaScript is awesome  ".trim().split(/\s+/).length);

Output:

3


30. Array to Object

console.log(Object.assign({}, ["a", "b", "c"]));

Output:

{ '0': 'a', '1': 'b', '2': 'c' }


31. Check Empty Object

console.log(Object.keys({}).length === 0);
console.log(Object.keys({a:1}).length === 0);

Output:

true
false


32. Merge Two Objects

console.log({ ...{a:1}, ...{b:2} });

Output:

{ a: 1, b: 2 }


33. Debounce Function

function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  }
}

const fn = debounce(() => console.log("Executed"), 500);
fn(); fn(); fn(); // Only last one runs

Output:

Executed


34. Throttle Function

function throttle(fn, delay) {
  let last = 0;
  return (...args) => {
    const now = Date.now();
    if (now - last >= delay) {
      last = now;
      fn.apply(this, args);
    }
  }
}

const fn = throttle(() => console.log("Called"), 1000);
fn(); fn(); fn();

Output:

Called


35. Shuffle Array

function shuffle(arr) {
  for (let i = arr.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    [arr[i], arr[j]] = [arr[j], arr[i]];
  }
  return arr;
}

console.log(shuffle([1,2,3,4,5]));

Output Example:

[3, 5, 1, 4, 2]


36. Object to Array

console.log(Object.entries({a:1, b:2}));

Output:

[ ['a',1], ['b',2] ]


37. Sum of Digits

function sumDigits(num) {
  return num.toString().split("")
    .reduce((a,b) => a + Number(b), 0);
}

console.log(sumDigits(987));

Output:

24


38. Same Digits Check

console.log(sameDigits(123, 321));
console.log(sameDigits(121, 112));

function sameDigits(a, b) {
  return a.toString().split("").sort().join("") === 
         b.toString().split("").sort().join("");
}

Output:

true
true


39. Clean Array (Remove Null/Empty)

console.log([0, null, undefined, 5, "", 3].filter(Boolean));

Output:

[5, 3]


40. Count Unique Values

console.log(new Set([1,2,2,3,4,4,5]).size);

Output:

5


41. Unique Elements

console.log([...new Set([1,2,2,3,4])]);

Output:

[1,2,3,4]


42. Find Duplicate Values

console.log([1,2,2,3,3,4].filter((v,i,arr)=>arr.indexOf(v)!==i));

Output:

[2,3]


43. Number to Binary

console.log((10).toString(2));

Output:

1010


44. Binary to Number

console.log(parseInt("1010", 2));

Output:

10


45. Range Sum

function rangeSum(a,b){
  let sum=0;
  for(let i=a;i<=b;i++) sum+=i;
  return sum;
}

console.log(rangeSum(1,5));

Output:

15


46. Armstrong Number

function isArmstrong(num){
  const digits = num.toString().split("");
  const power = digits.length;
  return digits.reduce((a,b)=>a+Math.pow(b,power),0)==num;
}

console.log(isArmstrong(153));

Output:

true


47. Longest Repeating Character

function longestRepeat(str){
  let max=0, curr=1;
  for(let i=1;i<str.length;i++){
    if(str[i]==str[i-1]) curr++;
    else curr=1;
    max = Math.max(max, curr);
  }
  return max;
}

console.log(longestRepeat("aaabbccccdde"));

Output:

4


48. Median of Array

function median(arr){
  arr.sort((a,b)=>a-b);
  let mid = Math.floor(arr.length/2);
  return arr.length%2? arr[mid] : (arr[mid-1]+arr[mid])/2;
}

console.log(median([1,3,4,2,6]));

Output:

3


49. Balanced Parentheses

function isBalanced(str){
  const stack=[];
  const map={')':'(', ']':'[', '}':'{'};
  for(let ch of str){
    if("({[".includes(ch)) stack.push(ch);
    else if(map[ch]!==stack.pop()) return false;
  }
  return stack.length===0;
}

console.log(isBalanced("{[()]}"));
console.log(isBalanced("{[(])}"));

Output:

true
false


50. Add Without + Operator

function add(a,b){
  while(b!==0){
    let carry = (a & b) << 1;
    a = a ^ b;
    b = carry;
  }
  return a;
}

console.log(add(5,7));

Output:

12


Leave a comment