Practice 100 JavaScript Basic Programming Questions, TechnoVlogs

Practice 100 JavaScript Basic Programming Questions


Q1. Write a JavaScript program to calculate the sum of two numbers.  
Input: 
let num1 = 5;
let num2 = 10;

Expected Output:  
15

Q2. Write a JavaScript program to check if a given number is even or odd.  
Input:  
let num = 7;

Expected Output:  
Odd

Q3. Write a JavaScript program to find the largest of three numbers.  
Input:  
let a = 12;
let b = 45;
let c = 32;

Expected Output:  
45

Q4. Write a JavaScript program to reverse a string.  
Input:  
let str = "hello";

Expected Output:  
"olleh"

Q5. Write a JavaScript program to check if a string is a palindrome.  
Input:  
let str = "radar";

Expected Output:  
true

Q6. Write a JavaScript program to print numbers from 1 to 10 using a loop.  

Expected Output:  
1
2
3
4
5
6
7
8
9
10

Q7. Write a JavaScript program to check if a given number is positive, negative, or zero.  
Input:  
let num = -3;
Expected Output:  
Negative

Q8. Write a JavaScript program to calculate the factorial of a number.  
Input:  
let num = 5;

Expected Output:  
120

Q9. Write a JavaScript program to print the multiplication table of a number.  
Input:  
let num = 3;

Expected Output:  
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Q10. Write a JavaScript program to find the sum of all numbers in an array.  
Input:  
let arr = [1, 2, 3, 4, 5];

Expected Output:  
15

Q11. Write a JavaScript program to find the largest number in an array.  
Input:  
let arr = [5, 12, 8, 20, 3];

Expected Output:  
20

Q12. Write a JavaScript program to count the number of vowels in a string.  
Input:  
let str = "hello world";

Expected Output:  
3

Q13. Write a JavaScript program to find the index of a specific value in an array.  
Input:  
let arr = [10, 20, 30, 40, 50];
let value = 30;

Expected Output:  
2

Q14. Write a JavaScript program to remove duplicate values from an array.  
Input:  
let arr = [1, 2, 2, 3, 4, 4, 5];

Expected Output:  
[1, 2, 3, 4, 5]

Q15. Write a JavaScript program to convert a temperature from Celsius to Fahrenheit.  
Input:  
let celsius = 25;

Expected Output:  
77

Q16. Write a JavaScript program to check if a number exists in an array.  
Input:  
let arr = [10, 20, 30, 40, 50];
let num = 25;

Expected Output:  
false

Q17. Write a JavaScript program to calculate the average of numbers in an array.  
Input:  
let arr = [10, 20, 30, 40, 50];

Expected Output:  
30

Q18. Write a JavaScript program to capitalize the first letter of each word in a string.  
Input:  
let str = "hello world";

Expected Output:  
"Hello World"

Q19. Write a JavaScript program to check if a number is a prime number.  
Input:  
let num = 7;

Expected Output:  
true

Q20. Write a JavaScript program to merge two arrays and remove duplicate elements.  
Input:  
let arr1 = [1, 2, 3];
let arr2 = [3, 4, 5];

Expected Output:  
[1, 2, 3, 4, 5]

Q21. Write a JavaScript program to calculate the power of a number.  
Input:  
let base = 2;
let exponent = 3;

Expected Output:  
8

Q22. Write a JavaScript program to find the GCD (Greatest Common Divisor) of two numbers.  
Input:  
let num1 = 12;
let num2 = 15;

Expected Output:  
3

Q23. Write a JavaScript program to check if a string contains a specific substring.  
Input:  
let str = "hello world";
let substring = "world";

Expected Output:  
true

Q24. Write a JavaScript program to sort an array in ascending order.  
Input:  
let arr = [3, 1, 4, 1, 5, 9];

Expected Output:  
[1, 1, 3, 4, 5, 9]

Q25. Write a JavaScript program to generate a random number between two values.  
Input:  
let min = 1;
let max = 10;

Expected Output:  
Random number between 1 and 10 (inclusive)

Q26. Write a JavaScript program to find the second largest number in an array.  
Input:  
let arr = [10, 20, 4, 45, 99];

Expected Output:  
45

Q27. Write a JavaScript program to count the number of occurrences of a specific element in an array.  
Input:  
let arr = [1, 2, 3, 1, 4, 1, 5];
let element = 1;

Expected Output:  
3

Q28. Write a JavaScript program to check if an array is sorted in ascending order.  
Input:  
let arr = [1, 2, 3, 4, 5];

Expected Output:  
true

Q29. Write a JavaScript program to calculate the sum of the digits of a number.  
Input:  
let num = 12345;

Expected Output:  
15

Q30. Write a JavaScript program to find the smallest number in an array.  
Input:  
let arr = [3, 1, 4, 1, 5, 9];

Expected Output:  
1

Q31. Write a JavaScript program to remove all falsy values from an array.  
Input:  
let arr = [0, 1, false, 2, "", 3, undefined];

Expected Output:  
[1, 2, 3]

Q32. Write a JavaScript program to find the length of the longest word in a string.  
Input:  
let str = "The quick brown fox jumps over the lazy dog";

Expected Output:  
5

Q33. Write a JavaScript program to generate the first `n` Fibonacci numbers.  
Input:  
let n = 5;

Expected Output:  
[0, 1, 1, 2, 3]

Q34. Write a JavaScript program to flatten a nested array.  
Input:  
let arr = [1, [2, [3, [4, 5]]]];

Expected Output:  
[1, 2, 3, 4, 5]

Q35. Write a JavaScript program to check if two arrays are equal.  
Input:  
let arr1 = [1, 2, 3];
let arr2 = [1, 2, 3];

Expected Output:  
true

Q36. Write a JavaScript program to convert a string to title case (capitalize each word).  
Input:  
let str = "javascript is fun";

Expected Output:  
"Javascript Is Fun"

Q37. Write a JavaScript program to find all unique values in an array.  
Input:  
let arr = [1, 2, 2, 3, 4, 4, 5];

Expected Output:  
[1, 2, 3, 4, 5]

Q38. Write a JavaScript program to check if a number is divisible by both 3 and 5.  
Input:  
let num = 15;

Expected Output:  
true

Q39. Write a JavaScript program to round a number to 2 decimal places.  
Input:  
let num = 3.14159;

Expected Output:  
3.14

Q40. Write a JavaScript program to count the number of words in a string.  
Input:  
let str = "The quick brown fox";

Expected Output:  
4

Q41. Write a JavaScript program to remove a specific element from an array.  
Input:  
let arr = [1, 2, 3, 4, 5];
let element = 3;

Expected Output:  
[1, 2, 4, 5]

Q42. Write a JavaScript program to calculate the sum of squares of numbers in an array.  
Input:  
let arr = [1, 2, 3, 4];

Expected Output:  
30

Q43. Write a JavaScript program to reverse the digits of a number.  
Input:  
let num = 12345;

Expected Output:  
54321

Q44. Write a JavaScript program to find the first non-repeating character in a string.  
Input:  
let str = "swiss";

Expected Output:  
"w"

Q45. Write a JavaScript program to find the sum of even numbers in an array.  
Input:  
let arr = [1, 2, 3, 4, 5, 6];

Expected Output:  
12

Q46. Write a JavaScript program to convert a number to its binary representation.  
Input:  
let num = 10;

Expected Output:  
"1010"

Q47. Write a JavaScript program to find the most frequent element in an array.  
Input:  
let arr = [1, 2, 2, 3, 3, 3, 4];

Expected Output:  
3

Q48. Write a JavaScript program to calculate the product of all numbers in an array.  
Input:  
let arr = [1, 2, 3, 4];

Expected Output:  
24

Q49. Write a JavaScript program to find the median of an array.  
Input:  
let arr = [1, 3, 3, 6, 7, 8, 9];

Expected Output:  
6

Q50. Write a JavaScript program to rotate an array `k` times to the right.  
Input:  
let arr = [1, 2, 3, 4, 5];
let k = 2;

Expected Output:  
[4, 5, 1, 2, 3]

Q51. Write a JavaScript program to check if a string is a palindrome.  
Input:  
let str = "radar";

Expected Output:  
true

Q52. Write a JavaScript program to count the frequency of each character in a string.  
Input:  
let str = "hello";

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

Q53. Write a JavaScript program to convert the first character of a string to uppercase.  
Input:  
let str = "javascript";

Expected Output:  
"Javascript"

Q54. Write a JavaScript program to generate a multiplication table for a given number.  
Input:  
let num = 5;

Expected Output:  
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

Q55. Write a JavaScript program to find the largest element in a 2D array.  
Input:  
let arr = [
 [1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]
];

Expected Output:  
9

Q56. Write a JavaScript program to calculate the factorial of a number using recursion.  
Input:  
let num = 5;

Expected Output:  
120

Q57. Write a JavaScript program to find the longest word in a string.  
Input:  
let str = "The quick brown fox jumped over the lazy dog";

Expected Output:  
"jumped"

Q58. Write a JavaScript program to check if all elements in an array are unique.  
Input:  
let arr = [1, 2, 3, 4, 5];

Expected Output:  
true

Q59. Write a JavaScript program to find the transpose of a 2D array.  
Input:  
let arr = [
 [1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]
];

Expected Output:  
[
 [1, 4, 7],
 [2, 5, 8],
 [3, 6, 9]
]

Q60. Write a JavaScript program to shuffle the elements of an array randomly.  
Input:  
let arr = [1, 2, 3, 4, 5];

Expected Output:  
Randomized array (e.g., [3, 5, 1, 2, 4])

Q61. Write a JavaScript program to find the difference between the maximum and minimum elements in an array.  
Input:  
let arr = [10, 20, 30, 40, 50];

Expected Output:  
40

Q62. Write a JavaScript program to capitalize the first letter of each word in a string.  
Input:  
let str = "javascript is awesome";

Expected Output:  
"Javascript Is Awesome"

Q63. Write a JavaScript program to merge two arrays and remove duplicate elements.  
Input:  
let arr1 = [1, 2, 3];
let arr2 = [2, 3, 4];

Expected Output:  
[1, 2, 3, 4]

Q64. Write a JavaScript program to check if a number is prime.  
Input:  
let num = 7;

Expected Output:  
true

Q65. Write a JavaScript program to reverse a string.  
Input:  
let str = "hello";

Expected Output:  
"olleh"

Q66. Write a JavaScript program to generate an array of random numbers of length `n`.  
Input:  
let n = 5;

Expected Output:  
[Random numbers, e.g., [4, 8, 2, 9, 1]]

Q67. Write a JavaScript program to find the second smallest element in an array.  
Input:  
let arr = [5, 3, 8, 1, 4];

Expected Output:  
3

Q68. Write a JavaScript program to check if a string ends with a specific substring.  
Input:  
let str = "hello world";
let substring = "world";

Expected Output:  
true

Q69. Write a JavaScript program to check if an object is empty.  
Input:  
let obj = {};

Expected Output:  
true

Q70. Write a JavaScript program to count the vowels in a string.  
Input:  
let str = "javascript";

Expected Output:  
3

Q71. Write a JavaScript program to find the greatest common divisor (GCD) of two numbers.  
Input:  
let num1 = 56;
let num2 = 98;

Expected Output:  
14

Q72. Write a JavaScript program to sort an array of strings alphabetically.  
Input:  
let arr = ["banana", "apple", "cherry"];

Expected Output:  
["apple", "banana", "cherry"]

Q73. Write a JavaScript program to check if a number is a power of 2.  
Input:  
let num = 16;

Expected Output:  
true

Q74. Write a JavaScript program to find the sum of the digits of a number.  
Input:  
let num = 1234;

Expected Output:  
10

Q75. Write a JavaScript program to check if two strings are anagrams.  
Input:  
let str1 = "listen";
let str2 = "silent";

Expected Output:  
true

Q76. Write a JavaScript program to find the index of the first occurrence of a number in an array.  
Input:  
let arr = [10, 20, 30, 40, 50];
let target = 30;

Expected Output:  
2

Q77. Write a JavaScript program to find the square root of a number without using the `Math.sqrt()` function.  
Input:  
let num = 16;

Expected Output:  
4

Q78. Write a JavaScript program to calculate the sum of the first `n` natural numbers.  
Input:  
let n = 5;

Expected Output:  
15

Q79. Write a JavaScript program to check if an array contains all odd numbers.  
Input:  
let arr = [1, 3, 5, 7];

Expected Output:  
true

Q80. Write a JavaScript program to find the largest number in an array without using built-in functions.  
Input:  
let arr = [10, 25, 5, 45, 30];

Expected Output:  
45

Q81. Write a JavaScript program to check if a string starts with a specific substring.  
Input:  
let str = "hello world";
let substring = "hello";

Expected Output:  
true

Q82. Write a JavaScript program to count the number of words in a string.  
Input:  
let str = "JavaScript is amazing";

Expected Output: 
3

Q83. Write a JavaScript program to find the cumulative sum of an array.  
Input:  
let arr = [1, 2, 3, 4];

Expected Output:  
[1, 3, 6, 10]

Q84. Write a JavaScript program to flatten a 2D array into a 1D array.  
Input:  
let arr = [
 [1, 2],
 [3, 4],
 [5, 6]
];

Expected Output:  
[1, 2, 3, 4, 5, 6]

Q85. Write a JavaScript program to remove all falsy values from an array.  
Input:  
let arr = [0, 1, false, 2, "", 3];

Expected Output:  
[1, 2, 3]

Q86. Write a JavaScript program to find the intersection of two arrays.  
Input:  
let arr1 = [1, 2, 3, 4];
let arr2 = [3, 4, 5, 6];

Expected Output:  
[3, 4]

Q87. Write a JavaScript program to split a string into an array of words.  
Input:  
let str = "Split this string";

Expected Output:  
["Split", "this", "string"]

Q88. Write a JavaScript program to find the length of the longest word in a string.  
Input:  
let str = "JavaScript is powerful";

Expected Output:  
10

Q89. Write a JavaScript program to repeat a string `n` times.  
Input:  
let str = "abc";
let n = 3;

Expected Output:  
"abcabcabc"

Q90. Write a JavaScript program to calculate the sum of the diagonal elements in a 2D square matrix.  
Input:  
let matrix = [
 [1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]
];

Expected Output:  
15

Q91. Write a JavaScript program to remove duplicate elements from an array.  
Input:  
let arr = [1, 2, 2, 3, 4, 4, 5];

Expected Output:  
[1, 2, 3, 4, 5]

Q92. Write a JavaScript program to count the occurrences of a specific element in an array.  
Input:  
let arr = [1, 2, 2, 3, 4, 2];
let target = 2;

Expected Output:  
3

Q93. Write a JavaScript program to reverse the elements of an array without using `reverse()`.  
Input:  
let arr = [1, 2, 3, 4, 5];

Expected Output:  
[5, 4, 3, 2, 1]

Q94. Write a JavaScript program to calculate the sum of even numbers in an array.  
Input:  
let arr = [1, 2, 3, 4, 5, 6];

Expected Output:  
12

Q95. Write a JavaScript program to find the first non-repeating character in a string.  
Input:  
let str = "swiss";

Expected Output:  
"w"

Q96. Write a JavaScript program to convert an array of numbers into an array of their squares.  
Input:  
let arr = [1, 2, 3, 4];

Expected Output:  
[1, 4, 9, 16]

Q97. Write a JavaScript program to check if a number is a perfect square.  
Input:  
let num = 25;

Expected Output:  
true

Q98. Write a JavaScript program to calculate the average of numbers in an array.  
Input:  
let arr = [10, 20, 30, 40];

Expected Output:  
25

Q99. Write a JavaScript program to rotate the elements of an array `n` times to the right.  
Input:  
let arr = [1, 2, 3, 4, 5];
let n = 2;

Expected Output:  
[4, 5, 1, 2, 3]

Q100. Write a JavaScript program to remove all instances of a specific element from an array.  
Input:  
let arr = [1, 2, 3, 2, 4, 2];
let target = 2;

Expected Output:  
[1, 3, 4]

Share on Social Media