20 Essential JavaScript Coding Questions – Numbers, Strings & Arrays

Practice the most commonly asked JavaScript questions for beginners. This list includes logic-building exercises like reversing numbers, checking prime, manipulating strings, sorting arrays, and detecting duplicates — perfect for interview prep and daily practice.

  • 1. Reverse a number

    Convert number to string, reverse it, and convert back to number, preserving the sign.

    function reverseNumber(n) {
      return parseInt(n.toString().split('').reverse().join('')) * Math.sign(n);
    }
    console.log(reverseNumber(123)); // 321
  • 2. Check if number is prime

    Loop from 2 to √n and check if any number divides n evenly.

    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;
    }
    console.log(isPrime(7)); // true
  • 3. Check if string contains only digits

    Use regular expression /^\d+$/ to test for only numeric characters.

    console.log(/^\d+$/.test("12345")); // true
  • 4. Capitalize first letter of each word

    Use \b\w regex to target first letter of each word and capitalize it.

    const str = "hello world";
    const capitalized = str.replace(/\b\w/g, c => c.toUpperCase());
    console.log(capitalized); // "Hello World"
  • 5. Check if array is sorted

    Compare each element with the previous one to ensure non-decreasing order.

    function isSorted(arr) {
      return arr.every((v, i, a) => i === 0 || a[i - 1] <= v);
    }
    console.log(isSorted([1, 2, 3])); // true
  • 6. Count vowels in string

    Match all vowels (case-insensitive) and count them using regex.

    const str = "Hello World";
    const vowelCount = str.match(/[aeiou]/gi)?.length || 0;
    console.log(vowelCount); // 3
  • 7. Sum of digits

    Convert number to string, split digits, convert back and sum them using reduce().

    const num = 123;
    const sum = num.toString().split('').reduce((a, b) => a + Number(b), 0);
    console.log(sum); // 6
  • 8. Check if object is empty

    Use Object.keys() to check if an object has any properties.

    const obj = {};
    console.log(Object.keys(obj).length === 0); // true
  • 9. Swap two variables without third

    Use array destructuring to swap variables without a temporary one.

    let a = 1, b = 2;
    [a, b] = [b, a];
    console.log(a, b); // 2 1
  • 10. Merge two arrays and remove duplicates

    Use spread operator to merge arrays, and Set to remove duplicates.

    const arr1 = [1, 2];
    const arr2 = [2, 3];
    const merged = [...new Set([...arr1, ...arr2])];
    console.log(merged); // [1, 2, 3]
  • 11. Find factorial of a number

    Use recursion to multiply the number by the factorial of the number minus one.

    function factorial(n) {
            return n <= 1 ? 1 : n * factorial(n - 1);
          }
    console.log(factorial(5)); // 120
  • 12. Check even or odd

    Use modulo operator (%) to determine if number is divisible by 2.

    const n = 7; 
    console.log(n % 2 === 0 ? 'Even' : 'Odd'); // "Odd"
  • 13. Count words in a string

    Trim and split the string on one or more spaces to count words.

    const str = "Hello world from JS";
    const wordCount = str.trim().split(/\s+/).length;
    console.log(wordCount); // 4
  • 14. Find largest number in array

    Use Math.max with spread operator to find the largest number.

    const arr = [3, 6, 2, 9];
          console.log(Math.max(...arr)); // 9
  • 15. Reverse each word in sentence

    Split sentence into words, reverse each word, then join them back.

    const str = "Hello World";
    const result = str.split(' ').map(w => w.split('').reverse().join('')).join(' ');
    console.log(result); // "olleH dlroW"
  • 16. Convert string to camelCase

    Replace dashes or underscores followed by characters with the uppercase version of that character.

    const str = "hello_world-test";
    const camel = str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase());
    console.log(camel); // "helloWorldTest"
  • 17. Check if string is uppercase

    Compare the string to its uppercase version.

    const str = "HELLO";
    console.log(str === str.toUpperCase()); // true
  • 18. Check if array contains duplicates

    Compare size of Set (unique elements) to original array length.

    const arr = [1, 2, 3, 3];
    console.log(new Set(arr).size !== arr.length); // true
  • 19. Sort array numerically

    Use a compare function (a - b) to sort numerically.

    const arr = [10, 2, 5];
    arr.sort((a, b) => a - b);
    console.log(arr); // [2, 5, 10]
  • 20. Get unique values from array

    Convert Set back to array using Array.from to remove duplicates.

    const arr = [1, 2, 2, 3];
    const unique = Array.from(new Set(arr));
    console.log(unique); // [1, 2, 3]