🧠 Top 25 JavaScript Programming Questions & Answers
1. Print “Hello, World!” in JavaScript
Code:
[Link]("Hello, World!");
Explanation:
[Link]() outputs text or variable values to the console.
2. Add Two Numbers
Code:
let a = 5, b = 10;
let sum = a + b;
[Link](sum);
Output: 15
3. Swap Two Variables Without Third Variable
Code:
let a = 10, b = 20;
[a, b] = [b, a];
[Link](a, b);
Output: 20 10
4. Check if a Number is Even or Odd
Code:
let num = 7;
if (num % 2 === 0) [Link]("Even");
else [Link]("Odd");
Output: Odd
5. Find the Largest of Three Numbers
Code:
let a = 10, b = 25, c = 20;
let largest = [Link](a, b, c);
[Link](largest);
Output: 25
6. Check if a String is a Palindrome
Code:
let str = "madam";
let reversed = [Link]("").reverse().join("");
[Link](str === reversed ? "Palindrome" : "Not Palindrome");
Output: Palindrome
7. Find Factorial of a Number
Code:
let num = 5;
let fact = 1;
for (let i = 1; i <= num; i++) fact *= i;
[Link](fact);
Output: 120
8. Reverse a String
Code:
let str = "JavaScript";
let reversed = [Link]("").reverse().join("");
[Link](reversed);
Output: tpircSavaJ
9. Count the Number of Vowels in a String
Code:
let str = "Hello World";
let count = [Link](/[aeiou]/gi).length;
[Link](count);
Output: 3
10. Check if a Number is Prime
Code:
let num = 13, isPrime = true;
for (let i = 2; i <= [Link](num); i++) {
if (num % i === 0) isPrime = false;
}
[Link](isPrime ? "Prime" : "Not Prime");
Output: Prime
11. Generate Fibonacci Series up to N Terms
Code:
let n = 6, a = 0, b = 1;
for (let i = 0; i < n; i++) {
[Link](a);
[a, b] = [b, a + b];
}
Output: 0 1 1 2 3 5
12. Find the Sum of Array Elements
Code:
let arr = [1, 2, 3, 4, 5];
let sum = [Link]((a, b) => a + b, 0);
[Link](sum);
Output: 15
13. Find the Maximum Element in an Array
Code:
let arr = [10, 45, 23, 78, 56];
[Link]([Link](...arr));
Output: 78
14. Remove Duplicates from an Array
Code:
let arr = [1, 2, 3, 2, 1];
let unique = [...new Set(arr)];
[Link](unique);
Output: [1, 2, 3]
15. Sort an Array in Ascending Order
Code:
let arr = [5, 3, 8, 1];
[Link]((a, b) => a - b);
[Link](arr);
Output: [1, 3, 5, 8]
16. Find Second Largest Number in Array
Code:
let arr = [10, 25, 47, 36];
let unique = [...new Set(arr)];
[Link]((a, b) => b - a);
[Link](unique[1]);
Output: 36
17. Check if a Character is a Vowel or Consonant
Code:
let ch = 'e';
if ('aeiou'.includes([Link]())) [Link]("Vowel");
else [Link]("Consonant");
Output: Vowel
18. Find the Length of a String Without Using .length
Code:
let str = "JavaScript";
let length = 0;
while (str[length] !== undefined) length++;
[Link](length);
Output: 10
19. Find the Sum of Digits of a Number
Code:
let num = 1234;
let sum = 0;
while (num > 0) {
sum += num % 10;
num = [Link](num / 10);
}
[Link](sum);
Output: 10
20. Find the Number of Occurrences of a Character
Code:
let str = "hello";
let count = [Link]('l').length - 1;
[Link](count);
Output: 2
21. Check Armstrong Number
Code:
let num = 153;
let sum = 0;
let temp = num;
while (temp > 0) {
let digit = temp % 10;
sum += digit ** 3;
temp = [Link](temp / 10);
}
[Link](sum === num ? "Armstrong" : "Not Armstrong");
Output: Armstrong
22. Find Intersection of Two Arrays
Code:
let a = [1, 2, 3, 4];
let b = [3, 4, 5, 6];
let result = [Link](x => [Link](x));
[Link](result);
Output: [3, 4]
23. Check Leap Year
Code:
let year = 2024;
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0)
[Link]("Leap Year");
else
[Link]("Not Leap Year");
Output: Leap Year
24. Count Words in a Sentence
Code:
let sentence = "I love JavaScript programming";
let count = [Link]().split(/\s+/).length;
[Link](count);
Output: 4
25. Reverse Words in a Sentence
Code:
let sentence = "I love JavaScript";
let reversed = [Link](" ").reverse().join(" ");
[Link](reversed);
Output: JavaScript love I
⚙️ Top 25 Intermediate–Advanced JavaScript
Programming Questions & Answers
1. Reverse an Integer Without Using String Methods
Code:
let num = 12345, rev = 0;
while (num !== 0) {
rev = rev * 10 + num % 10;
num = [Link](num / 10);
}
[Link](rev);
Output: 54321
2. Check if Two Strings Are Anagrams
Code:
let a = "listen", b = "silent";
let isAnagram = [Link]("").sort().join("") ===
[Link]("").sort().join("");
[Link](isAnagram);
Output: true
3. Find Missing Number in an Array (1 to n)
Code:
let arr = [1, 2, 3, 5];
let n = 5;
let missing = (n * (n + 1)) / 2 - [Link]((a, b) => a + b, 0);
[Link](missing);
Output: 4
4. Find Duplicates in an Array
Code:
let arr = [1, 2, 3, 2, 4, 1];
let dup = [Link]((v, i) => [Link](v) !== i);
[Link]([...new Set(dup)]);
Output: [2, 1]
5. Flatten a Nested Array
Code:
let arr = [1, [2, [3, [4]]]];
[Link]([Link](Infinity));
Output: [1, 2, 3, 4]
6. Remove Falsy Values from an Array
Code:
let arr = [0, 1, false, 2, '', 3];
arr = [Link](Boolean);
[Link](arr);
Output: [1, 2, 3]
7. Find Frequency of Each Element
Code:
let arr = [1, 2, 2, 3, 3, 3];
let freq = {};
[Link](n => freq[n] = (freq[n] || 0) + 1);
[Link](freq);
Output: {1:1, 2:2, 3:3}
8. Capitalize the First Letter of Each Word
Code:
let str = "hello javascript world";
let cap = [Link](/\b\w/g, c => [Link]());
[Link](cap);
Output: Hello Javascript World
9. Find All Prime Numbers Up to N
Code:
let n = 20;
let primes = [];
for (let i = 2; i <= n; i++) {
if ([...Array(i).keys()].slice(2).every(j => i % j !== 0))
[Link](i);
}
[Link](primes);
Output: [2, 3, 5, 7, 11, 13, 17, 19]
10. Check if Two Objects Are Equal
Code:
let obj1 = {a: 1, b: 2}, obj2 = {b: 2, a: 1};
[Link]([Link](obj1) === [Link](obj2));
Output: false
(order matters in JSON — better comparison below)
Better:
[Link](_.isEqual(obj1, obj2)); // Using lodash
11. Debounce Function Implementation
Code:
function debounce(fn, delay) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
Explanation: Prevents function from executing until after a delay period.
12. Throttle Function Implementation
Code:
function throttle(fn, delay) {
let last = 0;
return (...args) => {
let now = [Link]();
if (now - last >= delay) {
last = now;
fn(...args);
}
};
}
Explanation: Ensures the function executes only once per delay interval.
13. Create a Promise Example
Code:
const task = new Promise((resolve, reject) => {
setTimeout(() => resolve("Task Completed!"), 1000);
});
[Link]([Link]);
Output: Task Completed!
14. Fetch API Example
Code:
fetch("[Link]
.then(res => [Link]())
.then(data => [Link](data));
Explanation: Fetches data asynchronously from a fake API.
15. Async/Await Example
Code:
async function getData() {
let res = await
fetch("[Link]
let data = await [Link]();
[Link](data);
}
getData();
16. Find Unique Elements Without Set
Code:
let arr = [1, 2, 2, 3, 4, 4];
let unique = [Link]((v, i) => [Link](v) === i);
[Link](unique);
Output: [1, 2, 3, 4]
17. Find Common Keys in Two Objects
Code:
let obj1 = {a: 1, b: 2, c: 3};
let obj2 = {b: 4, c: 5, d: 6};
let common = [Link](obj1).filter(k => k in obj2);
[Link](common);
Output: ["b", "c"]
18. Sort Array of Objects by Property
Code:
let users = [{name: 'John', age: 25}, {name: 'Ava', age: 22}];
[Link]((a, b) => [Link] - [Link]);
[Link](users);
19. Check if Array Is Sorted
Code:
let arr = [1, 2, 3, 4];
let sorted = [Link]((v, i, a) => !i || a[i - 1] <= v);
[Link](sorted);
Output: true
20. Find the Longest Word in a String
Code:
let str = "JavaScript is a versatile language";
let longest = [Link](" ").reduce((a, b) => [Link] > [Link] ? a
: b);
[Link](longest);
Output: versatile
21. Find Intersection of Multiple Arrays
Code:
let arr1 = [1, 2, 3], arr2 = [2, 3, 4], arr3 = [3, 2];
let result = [Link](x => [Link](x) && [Link](x));
[Link](result);
Output: [2, 3]
22. Implement Array Shuffle
Code:
let arr = [1, 2, 3, 4];
[Link](() => [Link]() - 0.5);
[Link](arr);
23. Deep Clone an Object
Code:
let obj = {a: 1, b: {c: 2}};
let clone = structuredClone(obj);
[Link](clone);
24. Convert Object to Array of Key-Value Pairs
Code:
let obj = {a: 1, b: 2};
[Link]([Link](obj));
Output: [["a",1],["b",2]]
25. Count Occurrences of Words in a String
Code:
let str = "this is is a test test test";
let words = [Link](" ");
let freq = {};
[Link](w => freq[w] = (freq[w] || 0) + 1);
[Link](freq);
Output: {this:1, is:2, a:1, test:3}