Check for Subarray in JavaScript Last Updated : 17 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different approaches to check for Subarray in JavaScriptUsing String Conversion with join() - Best MethodThis approach converts both arrays into strings and checks if the subarray string is contained within the master array string. JavaScript function checkSub(a, sub) { return a.join(',').includes(sub.join(',')); } let a = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3] console.log(checkSub(a, [777, 22, 22])); OutputtrueUsing a Closure with indexOf()This approach uses a closure to keep track of the index for searching within the master array. It ensures that the elements of the subarray appear in the same order within the master array. JavaScript function subArray(a, sub) { return sub.every((i => v => i = a.indexOf(v, i) + 1)(0)); } const a = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3]; console.log(subArray(a, [777, 22, 22])); OutputtrueSliding Window TechniqueThis approach slides over the master array using a window that matches the size of the subarray and checks for element-wise equality. JavaScript function subArray(a, sub) { const subLength = sub.length; for (let i = 0; i <= a.length - subLength; i++) { let isMatch = true; for (let j = 0; j < subLength; j++) { if (a[i + j] !== sub[j]) { isMatch = false; break; } } if (isMatch) return true; } return false; } let a = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3] console.log(subArray(a, [777, 22, 22])); OutputtrueUsing Functional ApproachThis approach uses Array.prototype.every() combined with indexOf() to ensure the order is preserved while checking for elements. JavaScript function isSubArray(main, sub) { let index = -1; return sub.every( (element) => (index = main.indexOf(element, index + 1)) !== -1 ); } const a = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3]; console.log(isSubArray(a, [777, 22, 22])); OutputtrueWhich Approach To Use?String Conversion: Best Method in terms of efficiency and usageClosure with indexOf(): Best for simple subarray checks with order preservation.Functional Approach: Concise and maintains order.Sliding Window: More robust and efficient for large arrays with repeated elements. Comment More infoAdvertise with us Next Article Check for Subarray in JavaScript S souravsharma098 Follow Improve Article Tags : JavaScript Web Technologies javascript-array Similar Reads JavaScript - Check if JS Array Includes a Value? To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false.1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the s 4 min read JavaScript typedArray.find() with Example The Javascript typedArray.find() is an inbuilt function in JavaScript that is used to return a value in the typedArray, if the value satisfies the condition given in the function, otherwise, it returns undefined. Syntax: typedArray.find(callback) Parameters: It takes the parameter âcallbackâ functio 2 min read How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript 2 min read How to Check Object is an Array in JavaScript? There are two different approaches to check an object is an array or not in JavaScript.1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false. 1 min read Check if an array is empty or not in JavaScript These are the following ways to check whether the given array is empty or not:1. The length Property - mostly usedThe length property can be used to get the length of the given array if it returns 0 then the length of the array is 0 means the given array is empty else the array have the elements.Jav 1 min read JavaScript typedArray.filter() with Example The Javascript typedArray.filter() is an inbuilt function in javascript that is used to form a new typedArray with the elements which satisfy the test implemented by the function provided. Syntax: typedarray.filter(callback) Parameters: It takes the parameter "callback" function which checks each el 2 min read JavaScript typedArray.findIndex() with Example The Javascript typedArray.findIndex() is an inbuilt function in JavaScript that is used to return an index in the tyedArray if the value satisfies the condition given in the function, otherwise, it returns -1. Syntax: typedarray.findIndex(callback) Parameters: It takes the parameter âcallbackâ funct 2 min read JavaScript - Search An Item in an Array Here are the different methods to search for an item in an array in JavaScript1. Using indexOf()The indexOf() method searches for a specified value in an array and returns its first index. If the value is not found, it returns -1.JavaScriptconst a = [10, 20, 30, 40]; const index = a.indexOf(30); con 3 min read Convert an Array into Array of Subarrays using JavaScript Given an array Num, our task is to convert it into an array of subarrays. Each subarray should contain a single element from the original array. we are going to discuss different approaches for solving this problem.Example:Input: Num=[1, 2, 3, 4] Output:[[1], [2], [3], [4]]Below are the approaches t 3 min read JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth 3 min read Like