var array2 = [3,6,7,8,1];
array2.slice(1);

will produce [6, 7, 8, 1]

Note that slice copies the array's contents only one level deep. What that means technically is that nested arrays or objects within the output of slice are still references to the same arrays or objects contained in the original parent array. What that means to you practically is that changing an element or property in the nested arrays/objects returned from slice will also change the same element or property within the nested array or object contained in the original parent array.

Answer from Joseph Myers on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
The substring() method extracts characters, between two indices (positions), from a string, and returns the substring.
Discussions

javascript - How do you search an array for a substring match? - Stack Overflow
I need to search an array in JavaScript. The search would be for only part of the string to match as the string would have additional components. I would then need to return the successfully matched More on stackoverflow.com
🌐 stackoverflow.com
September 1, 2018
Is there a javascript method to find substring in an array of strings? - Stack Overflow
Suppose my array is ["abcdefg", "hijklmnop"] and I am looking at if "mnop" is part of this array. How can I do this using a javascript method? I tried this and it does not work: var array= ["abcd... More on stackoverflow.com
🌐 stackoverflow.com
How do I only replace the first instance of a substring?
Solved. In case you also need it, and you're using regexes: string = string.left(regex_match.get_start()) + (string.right(string.length() - regex_match.get_end())) This basically just adds the string that's to the left of the match to the string that's to the right of it, skipping over it. More on reddit.com
🌐 r/godot
2
1
March 31, 2024
ELI5: Using .map and .substring
Some classes jump ahead way too quickly which is unfortunate, I remember when I first started taking the codecademy "learn JS" class, they immediately jumped into jQuery and React just a lesson after teaching what a function was which in hindsight is totally absurd. As a basic first step, whenever you have trouble try looking over the MDN page for the topic . I don't know why this is so weird to me, but it seems really weird to declare the const, and then pass it a function with another function inside of it. Sure, but this is probably because you've yet to work with any complex data, or need to make a lot of complex changes to data in sequence where you may not be in control of the input. It might help to demonstrate more about map itself: // A **very** simplified version of what Array.map would be, // though you shouldn't modify the prototype like this: Array.prototype.map = function (callback) { // ^ We're passing a function in as an argument named callback let resultArray = []; // But otherwise it's just syntactical sugar for a normal for loop: for ( let i = 0; i < this.length; // "this" equals the array here, like [1,2,3] i++ ) resultArray.push(callback(this[i], i, this)); // ^ read as (value, index, array) which we're passing into the callback function, // returning the result of that callback no matter what it may be return resultArray; }; // This allows us to easily modify any list and return a new array on the fly: const foo = [1, 2, 3]; const timesTwo = foo.map(function (value, index, array) { return value * 2; }); console.log(timesTwo); // [2, 4, 6] const shortAndSweet = foo.map((v) => v * 2); // [2, 4, 6] // v is arbitrary. It can be named whatever I want as long as it's the first argument, it corresponds to this[i]/value in the manual Map function above const longAndAnnoyingToWrite = []; for (var i = 0; i < foo.length; i++) longAndAnnoyingToWrite.push(foo[i] * 2) This may be overwhelming to look at, so let's boil it down to look more traditional: // Why not just make a custom function to do what we wanted to use map for in the first place? function multiplyArrayTimesTwo(array) { let resultArray = []; for (let i = 0; i < array.length; i++) resultArray.push(array[i] * 2); return resultArray; } // Or turn map into a normal looking function: function mapArray(array, callback) { // ^ Still containing the callback function as an argument though, why? let resultArray = []; for (let i = 0; i < array.length; i++) resultArray.push(callback(array[i], i, array)); return resultArray; } // We can produce the same results: const foo = [1, 2, 3]; const timesTwo = mapArray(foo, function (value, index, array) { return value * 2; }); const timesTwoAgain = multiplyArrayTimesTwo(foo) console.log(timesTwo, timesTwoAgain); // [2, 4, 6] , [2, 4, 6] const shortAndSweet = mapArray(foo, (v) => v * 2); // [2, 4, 6] Why not just do this? The answer is really only that we may want to modify this array in many ways besides multiplying by only two. We may need to multiply it by two once, but we may need to also change it in a variety of other ways, and we can more easily do that with callback functions than by writing out a custom function for every single change we'd want to make. const alphabet = "abcdefghijklmnopqrstuvwxyz"; const foo = [1, 2, 3]; const timesTwoAsLettersWithOtherStuff = foo .map((value) => value * 2) // Multiply it by two > [2, 4, 6] .map((value) => alphabet[value]) // Then change it to a letter > ["c", "e", "g"] .map((value) => value.padStart(4, "0")); // Add leading 0 until the total length is four > ["000c", "000e", "000g"] const short = foo.map(i => i * 2).map(i => alphabet[i]).map(i => i.padStart(5, "0")); If we had to write the above out with a custom function to do each step, we'd be looking at far more lines of code (repeating the for loop each one) instead of only 2 or 3 lines. And if we're not using arrow functions, we can again run into having what would otherwise be concise code as very vertical: const timesTwoAsLettersWithOtherStuff = foo .map(function (i) { return i * 2; // Multiply it by two > [2, 4, 6] }) .map(function (i) { return alphabet[i]; // Then change it to a letter > ["c", "e", "g"] }) .map(function (i) { return i.padStart(4, "0"); // Add leading 0 until the total length is four > ["000c", "000e", "000g"] }); This isn't the most practical example because you don't need 3 separate map functions to do the above. You could do it in a single one via map(i => alphabet[i * 2].padStart(4, "0")), but the principle is that you'll find yourself using several Array methods in a chain to do complex operations with map being only one of these: const someComplexModification = foo .map(/* ... */) .filter(/* ... */) .sort(/* ... */) .map(/* ... */) .forEach(/* ... */); // You wouldn't do this though, because forEach doesn't return values // Which if we built a custom function that needed us to supply the array as an argument would have to look like this: const complexAndOverlyComplicated = doForEach( doMap(doSort(doFilter(doMap(foo)))) // Ooof, gross ); // Or a chain like this: const mapped = doMap(foo); const filtered = doFilter(mapped); const sorted = doSort(filtered); const mappedAgain = doMap(sorted); for (let index = 0; index < mappedAgain.length; index++) { let value = mappedAgain[index]; // Now we finally do what we wanted, but have a ton of redundant variables declared and code above } Finally, the fact that the argument in the anonymous function, here: "j", seems so arbitrary and I don't understand why. Does it have anything to do with the fact that .map is already being called on the emojipedia array, and since "j" is an extremely local variable, it doesn't matter? It is arbitrary. It doesn't matter what it's named, it only matters what the argument position is. If it's the first argument, it's always the value because the parameters are (value, index, array). You can name it whatever you want but people tend to do one-letter characters so the code is the most concise, though it isn't always helpful to do this especially when showing it to others. I decided to in the above specifically to demonstrate to you that it was arbitrary. More on reddit.com
🌐 r/learnjavascript
8
2
March 24, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › check-an-array-of-strings-contains-a-substring-in-javascript
Check If An Array of Strings Contains a Substring in JavaScript - GeeksforGeeks
February 27, 2026 - The code uses Lodash's _.strContains() method from the lodash-contrib library to check if the string "abc" contains the substring "a". The result is stored in the variable bool. Using a for of loop and indexOf iterates through each string in the array.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript - MDN Web Docs
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
Top answer
1 of 14
105

People here are making this waaay too difficult. Just do the following...

myArray.findIndex(element => element.includes("substring"))

findIndex() is an ES6 higher order method that iterates through the elements of an array and returns the index of the first element that matches some criteria (provided as a function). In this case I used ES6 syntax to declare the higher order function. element is the parameter of the function (which could be any name) and the fat arrow declares what follows as an anonymous function (which does not need to be wrapped in curly braces unless it takes up more than one line).

Within findIndex() I used the very simple includes() method to check if the current element includes the substring that you want.

2 of 14
94

If you're able to use Underscore.js in your project, the _.filter() array function makes this a snap:

// find all strings in array containing 'thi'
var matches = _.filter(
    [ 'item 1', 'thing', 'id-3-text', 'class' ],
    function( s ) { return s.indexOf( 'thi' ) !== -1; }
);

The iterator function can do whatever you want as long as it returns true for matches. Works great.

Update 2017-12-03:
This is a pretty outdated answer now. Maybe not the most performant option in a large batch, but it can be written a lot more tersely and use native ES6 Array/String methods like .filter() and .includes() now:

// find all strings in array containing 'thi'
const items = ['item 1', 'thing', 'id-3-text', 'class'];
const matches = items.filter(s => s.includes('thi'));

Note: There's no <= IE11 support for String.prototype.includes() (Edge works, mind you), but you're fine with a polyfill, or just fall back to indexOf().

🌐
Refine
refine.dev › home › blog › tutorials › javascript substring method
JavaScript Substring Method | Refine
January 1, 2025 - In the end, we point out how JavaScript ... String.prototype.substr(). ... Array.prototype.substring() takes two possible arguments: a startIndex and an endIndex....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-string-substring-method
JavaScript String substring() Method | GeeksforGeeks
November 28, 2024 - If only the starting index is provided, substring() will return the substring from that index to the end of the string. ... Like most string methods in JavaScript, substring() does not alter the original string.
Find elsewhere
🌐
Squash
squash.io › javascript-substring-splice-and-slice-the-complete-guide
How to Use Javascript Substring, Splice, and Slice
May 1, 2023 - Related Article: How to Remove a Specific Item from an Array in JavaScript · Javascript substring is a method used to extract a portion of a string. It takes two arguments: the starting index and the ending index.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › split
String.prototype.split() - JavaScript - MDN Web Docs
The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
🌐
Edureka
edureka.co › blog › javascript-substring
JavaScript Substring() | How is it different from substr() & slice()? | Edureka
January 9, 2025 - This Edureka blog is about the JavaScript substring() whih is an inbuilt function that returns a part of the given string from start index to end index.
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how to split a string into substrings in javascript
How to Split a String into Substrings in JavaScript — SitePoint
November 6, 2024 - The split() method divides a string into an array of substrings based on a specified separator and returns the new array. On the other hand, the substring() method extracts characters from a string between two specified indices and returns the ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-substring-examples-slice-substr-and-substring-methods-in-js
JavaScript Substring Examples - Slice, Substr, and Substring Methods in JS
March 22, 2020 - Note: We can use the slice( ) method also for JavaScript arrays. You can find here my other article about the slice method to see the usage for arrays. According to the Mozilla documents, the substr( ) method is considered a legacy function and its use should be avoided.
🌐
ReqBin
reqbin.com › code › javascript › i5foejaa › javascript-substring-example
How do I get a substring from a string in JavaScript?
To get a substring from a string in JavaScript, you can use the built-in string.substring(start, end) method. The first parameter specifies the index of the first character from which to get the substring (in JavaScript, indexing starts from zero).
🌐
xjavascript
xjavascript.com › blog › how-do-you-search-an-array-for-a-substring-match
How to Search a JavaScript Array for Substring Matches and Return Full Elements — xjavascript.com
In JavaScript, arrays are a fundamental data structure used to store collections of values. A common task is searching an array to find elements that contain a specific substring (e.g., filtering product names containing "apple" from a list, ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.substring()
JavaScript Substring(): Extract a Substring from a String
November 3, 2024 - Then the substring returns the domain that starts from the index of @ plus 1 to the end of the string. The JavaScript substring() method returns the substring from the start index up to and excluding the end index.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › slice
String.prototype.slice() - JavaScript - MDN Web Docs
If indexStart < 0, the index is counted from the end of the string. More formally, in this case, the substring starts at max(indexStart + str.length, 0).
🌐
Programiz
programiz.com › javascript › library › string › substring
Javascript String substring()
The substring() method returns a specified part of the string between start and end indexes.