stringto search for - required
lengthof the string - optional - when specified, the length of the string will be adjusted only for the method operation
📺 The video in this post was made by Programming with Mosh
The origin of the article: https://www.youtube.com/watch?v=09BwruU4kiY&list=PLTjRvDozrdlxEIuOBZkMAK5uiqp8rHUax&index=6
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!
**length** property returns the length of a string. Take a look at the example below:
The Length method.
You can search for a string inside another string using the search method, It will return the position of that string. Have a look at the example below:
The Search method.
Array prototype objects.
The function is used to return the index (position) of the last occurrence of the given argument.
0, and here are some examples of using the
// A string let aString = "Good morning! It's a great morning to learn programming."; let index = aString.lastIndexOf("morning"); console.log(index); // 27 // An array let anArray = ["day", "night", "dawn", "day"]; let valueIndex = anArray.lastIndexOf("day"); console.log(valueIndex); // 3
As you can see from the examples above, the
lastIndexOf() method is available for
When the value you search for is not found in the string or array, the method will return
let aString = "Nathan Sebhastian"; let index = aString.lastIndexOf("morning"); console.log(index); // -1 let anArray = ["day", "night", "dawn", "day"]; let valueIndex = anArray.lastIndexOf("dusk"); console.log(valueIndex); // -1
lastIndexOf() method is also case sensitive, so you need to have the letter case in the argument match the actual string.
The above example returns
Nathan is different from
let aString = "Nathan Sebhastian"; let index = aString.lastIndexOf("nathan"); console.log(index); // -1
lastIndexOf() method will search your string or array backward.
This means the method will look for matching values from the last element and ends with the first element.
Finally, the method also accepts a second parameter to define the start position of the search.
The following example shows how to start the
lastIndexOf() search at index
let aString = "Hello World!"; let index = aString.lastIndexOf("World", 4); console.log(index); // -1
Because a second argument
4 is passed to the
4 to index
World string is available inside the
aString variable, the index starts at
6. Since it’s outside the search range of the
lastIndexOf() method, the value is not found and
-1 is returned.
The same rule applies when you search for a value in an array:
let anArray = [8, 3, 4, 8, 2]; let index = anArray.lastIndexOf(8, 2); console.log(index); // 0
In the abov example, the
lastIndexOf() method returns
0 for the index of the value
This is because the second argument
2 makes the method search from index
2 only. Since the last index of the value(
3, it stays outside of the method search range.
And that’s how the
Original article source at: https://sebhastian.com/