Practice 50 JavaScript Array Programming Questions
Q1. Write a JavaScript program to find the length of an array.
Input: arr = [1, 2, 3, 4]
Expected Output: 4
Q2. Write a JavaScript program to add an element to the end of an array.
Input: arr = [1, 2, 3], element = 4
Expected Output: [1, 2, 3, 4]
Q3. Write a JavaScript program to add an element to the beginning of an array.
Input: arr = [2, 3, 4], element = 1
Expected Output: [1, 2, 3, 4]
Q4. Write a JavaScript program to remove the last element of an array.
Input: arr = [1, 2, 3, 4]
Expected Output: [1, 2, 3]
Q5. Write a JavaScript program to remove the first element of an array.
Input: arr = [1, 2, 3, 4]
Expected Output: [2, 3, 4]
Q6. Write a JavaScript program to find the index of an element in an array.
Input: arr = [10, 20, 30, 40], element = 30
Expected Output: 2
Q7. Write a JavaScript program to check if an array contains a specific element.
Input: arr = [1, 2, 3, 4], element = 3
Expected Output: true
Q8. Write a JavaScript program to find the largest number in an array.
Input: arr = [10, 20, 4, 45, 99]
Expected Output: 99
Q9. Write a JavaScript program to find the smallest number in an array.
Input: arr = [10, 20, 4, 45, 99]
Expected Output: 4
Q10. Write a JavaScript program to sort an array in ascending order.
Input: arr = [4, 2, 5, 1, 3]
Expected Output: [1, 2, 3, 4, 5]
Q11. Write a JavaScript program to sort an array in descending order.
Input: arr = [4, 2, 5, 1, 3]
Expected Output: [5, 4, 3, 2, 1]
Q12. Write a JavaScript program to reverse an array.
Input: arr = [1, 2, 3, 4]
Expected Output: [4, 3, 2, 1]
Q13. Write a JavaScript program to join two arrays.
Input: arr1 = [1, 2], arr2 = [3, 4]
Expected Output: [1, 2, 3, 4]
Q14. Write a JavaScript program to find the sum of all elements in an array.
Input: arr = [1, 2, 3, 4]
Expected Output: 10
Q15. Write a JavaScript program to find the average of all elements in an array.
Input: arr = [1, 2, 3, 4]
Expected Output: 2.5
Q16. Write a JavaScript program to remove a specific element from an array.
Input: arr = [1, 2, 3, 4], element = 3
Expected Output: [1, 2, 4]
Q17. Write a JavaScript program to concatenate two arrays.
Input: arr1 = [1, 2], arr2 = [3, 4]
Expected Output: [1, 2, 3, 4]
Q18. Write a JavaScript program to check if an array is empty.
Input: arr = []
Expected Output: true
Q19. Write a JavaScript program to find the median of an array.
Input: arr = [1, 2, 3, 4, 5]
Expected Output: 3
Q20. Write a JavaScript program to find the mode of an array.
Input: arr = [1, 2, 3, 3, 4]
Expected Output: 3
Q21. Write a JavaScript program to find the unique elements in an array.
Input: arr = [1, 2, 2, 3, 4, 4]
Expected Output: [1, 2, 3, 4]
Q22. Write a JavaScript program to merge two sorted arrays.
Input: arr1 = [1, 4, 6], arr2 = [2, 3, 5]
Expected Output: [1, 2, 3, 4, 5, 6]
Q23. Write a JavaScript program to find the intersection of two arrays.
Input: arr1 = [1, 2, 3], arr2 = [2, 3, 4]
Expected Output: [2, 3]
Q24. Write a JavaScript program to find the union of two arrays.
Input: arr1 = [1, 2, 3], arr2 = [3, 4, 5]
Expected Output: [1, 2, 3, 4, 5]
Q25. Write a JavaScript program to find the difference between two arrays.
Input: arr1 = [1, 2, 3], arr2 = [2, 3, 4]
Expected Output: [1]
Q26. Write a JavaScript program to find the second largest number in an array.
Input: arr = [1, 2, 3, 4, 5]
Expected Output: 4
Q27. Write a JavaScript program to find the second smallest number in an array.
Input: arr = [1, 2, 3, 4, 5]
Expected Output: 2
Q28. Write a JavaScript program to duplicate each element in an array.
Input: arr = [1, 2, 3]
Expected Output: [1, 1, 2, 2, 3, 3]
Q29. Write a JavaScript program to find the difference between two arrays.
Input: arr1 = [1, 2, 3], arr2 = [3, 4, 5]
Expected Output: [1, 2]
Q30. Write a JavaScript program to convert a string into an array of characters.
Input: str = "hello"
Expected Output: ["h", "e", "l", "l", "o"]
Q31. Write a JavaScript program to check if an array is a subset of another array.
Input: arr1 = [1, 2, 3], arr2 = [1, 2]
Expected Output: true
Q32. Write a JavaScript program to find the number of occurrences of a specific element in an array.
Input: arr = [1, 2, 3, 1, 1, 4], element = 1
Expected Output: 3
Q33. Write a JavaScript program to check if all elements of an array are unique.
Input: arr = [1, 2, 3, 4, 5]
Expected Output: true
Q34. Write a JavaScript program to remove all occurrences of a specific element from an array.
Input: arr = [1, 2, 3, 4, 2], element = 2
Expected Output: [1, 3, 4]
Q35. Write a JavaScript program to find the common elements between three arrays.
Input: arr1 = [1, 2, 3], arr2 = [2, 3, 4], arr3 = [3, 4, 5]
Expected Output: [3]
Q36. Write a JavaScript program to create an array of even numbers from 1 to 100.
Input: range = 100
Expected Output: [2, 4, 6, 8, ..., 100]
Q37. Write a JavaScript program to create an array of odd numbers from 1 to 100.
Input: range = 100
Expected Output: [1, 3, 5, 7, ..., 99]
Q38. Write a JavaScript program to calculate the product of all elements in an array.
Input: arr = [1, 2, 3, 4]
Expected Output: 24
Q39. Write a JavaScript program to find the first repeated element in an array.
Input: arr = [1, 2, 3, 4, 1]
Expected Output: 1
Q40. Write a JavaScript program to check if two arrays are equal.
Input: arr1 = [1, 2, 3], arr2 = [1, 2, 3]
Expected Output: true
Q41. Write a JavaScript program to create an array of multiples of a number.
Input: num = 3, range = 5
Expected Output: [3, 6, 9, 12, 15]
Q42. Write a JavaScript program to find the largest even number in an array.
Input: arr = [1, 2, 3, 4, 5]
Expected Output: 4
Q43. Write a JavaScript program to find the largest odd number in an array.
Input: arr = [1, 2, 3, 4, 5]
Expected Output: 5
Q44. Write a JavaScript program to generate an array of random numbers.
Input: length = 5, min = 1, max = 100
Expected Output: [random values between 1 and 100]
Q45. Write a JavaScript program to find the sum of squares of all elements in an array.
Input: arr = [1, 2, 3, 4]
Expected Output: 30
Q46. Write a JavaScript program to check if an array contains only numbers.
Input: arr = [1, 2, "three", 4]
Expected Output: false
Q47. Write a JavaScript program to find the largest prime number in an array.
Input: arr = [1, 2, 3, 4, 5, 6]
Expected Output: 5
Q48. Write a JavaScript program to find the average of even numbers in an array.
Input: arr = [1, 2, 3, 4, 5, 6]
Expected Output: 4
Q49. Write a JavaScript program to check if an array contains a sequence of numbers.
Input: arr = [1, 2, 3, 4], sequence = [2, 3]
Expected Output: true
Q50. Write a JavaScript program to convert an array into a string with a custom delimiter.
Input: arr = [1, 2, 3], delimiter = "-"
Expected Output: "1-2-3"
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!