feat: add article for JavaScript String.lastIndexOf() (#36013)

* feat: add article for JavaScript String.lastIndexOf()

* Update guide/english/javascript/standard-objects/string/string-prototype-lastindexof/index.md

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
This commit is contained in:
Hasan Abdullah
2019-07-06 12:09:27 -04:00
committed by Tom
parent 9fe78e3827
commit ef7a13dda3

View File

@ -3,13 +3,41 @@ title: String.prototype.lastIndexOf
--- ---
## String.prototype.lastIndexOf ## String.prototype.lastIndexOf
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/javascript/standard-objects/string/string-prototype-lastindexof/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>. The `lastIndexOf()` method returns the first index at which the last occurrence of a specified string can be found in the given String object. If the string is not present, it returns -1.
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>. **Syntax**
```javascript
str.lastIndexOf(searchValue[, fromIndex])
```
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds --> ### Parameters
* **searchValue** Substring for which you are looking. If this is empty (`''`) and there is no `fromIndex` argument, `lastIndexOf()` will return the string's length.
#### More Information: * **fromIndex** Optional. The index at which you want to start the search backwards from. The default value is the string's length.
<!-- Please add any articles you think might be helpful to read before writing the article --> * If `fromIndex` is negative, the method will behave as though it were 0.
* If `fromIndex` is less than the string's length:
* If `searchValue` is not an empty string (`''`), the string will be searched backwards from `fromIndex`.
* If `searchValue` is an empty string (`''`), the method returns `fromIndex`.
* If `fromIndex` is greater than or equal to the string's length:
* If `searchValue` is not an empty string (`''`), the method will behave as though it were the string's length.
* If `searchValue` is an empty string (`''`), the method returns the string's length.
### Description
The `lastIndexOf()` method checks the string from right to left. The index of the first character is 0 and the index of the last character is ``string.length - 1``. The method checks each substring against `searchValue` using strict equality (`===`), which means that this method is case sensitive. Once it finds a substring that returns `true`, it returns the index of its first character.
### Examples
```javascript
'Blue Whale'.lastIndexOf('Blue'); // returns 0
'Blue Whale'.lastIndexOf('Blute'); // returns -1
'Blue Whale'.lastIndexOf('blue'); // returns -1
'Blue Whale'.lastIndexOf('Whale', 0); // returns -1
'Blue Whale'.lastIndexOf('Whale', 5); // returns 5
'Blue Whale'.lastIndexOf('Whale', 7); // returns 5
'Blue Whale'.lastIndexOf(''); // returns 10
'Blue Whale'.lastIndexOf('', 9); // returns 9
'Blue Whale'.lastIndexOf('', 10); // returns 10
'Blue Whale'.lastIndexOf('', 11); // returns 10
```
### More Information:
- [String.prototype.lastIndexOf() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf)