fix: update article for JavaScript String.indexOf() (#36012)

* fix: update article for JavaScript String.indexOf()

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

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

View File

@ -2,7 +2,8 @@
title: String.prototype.indexOf title: String.prototype.indexOf
--- ---
## String.prototype.indexOf ## String.prototype.indexOf
The `indexOf()` method returns the first index at which a given element can be found in the array. If the element is not present, it returns -1.
The `indexOf()` method returns the first index at which a specified string can be found in the given String object. If the string is not present, it returns -1.
**Syntax** **Syntax**
```javascript ```javascript
@ -10,17 +11,25 @@ str.indexOf(searchValue[, fromIndex])
``` ```
### Parameters ### Parameters
* **searchValue** Substring for which you are looking. If this is empty (`''`) and there is no `fromIndex` parameter, this will return 0. * **searchValue** Substring for which you are looking. If this is empty (`''`) and there is no `fromIndex` argument, `indexOf()` will return 0.
* **fromIndex** Optional. The index at which you want to start the search. If the `fromIndex` value is greater than or equal to the string's length, the string is not searched and the method returns -1. If the `searchValue` is an empty string (`''`) and the `fromIndex` is less than the string's length, it will return the `fromIndex`; otherwise, it will return the string's length. (A negative number will be treated as though there is no argument.) * **fromIndex** Optional. The index at which you want to start the search from. The default value is 0.
* If `fromIndex` is negative, the string will be searched from the beginning.
* If `fromIndex` is less than the string's length:
* If `searchValue` is not an empty string (`''`), the string will be searched 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 string is not searched and the method returns -1.
* If `searchValue` is an empty string (`''`), the method returns the string's length.
### Description ### Description
The `indexOf()` method checks the string from left to right. The index of the first character is 0; the index of the last character is ``string.length - 1``. The method checks each substring against `searchValue` using strict equality (`===`), which means this method is case sensitive. Once it finds a substring that returns `true`, it returns the index of its first character. The `indexOf()` method checks the string from left to right. 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 ### Examples
```javascript ```javascript
'Blue Whale'.indexOf('Blue'); // returns 0 'Blue Whale'.indexOf('Blue'); // returns 0
'Blue Whale'.indexOf('Blute'); // returns -1 'Blue Whale'.indexOf('Blute'); // returns -1
'Blue Whale'.indexOf('blue'); // returns -1
'Blue Whale'.indexOf('Whale', 0); // returns 5 'Blue Whale'.indexOf('Whale', 0); // returns 5
'Blue Whale'.indexOf('Whale', 5); // returns 5 'Blue Whale'.indexOf('Whale', 5); // returns 5
'Blue Whale'.indexOf('Whale', 7); // returns -1 'Blue Whale'.indexOf('Whale', 7); // returns -1
@ -28,9 +37,7 @@ The `indexOf()` method checks the string from left to right. The index of the fi
'Blue Whale'.indexOf('', 9); // returns 9 'Blue Whale'.indexOf('', 9); // returns 9
'Blue Whale'.indexOf('', 10); // returns 10 'Blue Whale'.indexOf('', 10); // returns 10
'Blue Whale'.indexOf('', 11); // returns 10 'Blue Whale'.indexOf('', 11); // returns 10
'Blue Whale'.indexOf('blue'); // returns -1
``` ```
### More Information: ### More Information:
- MDN documentation: <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf' target='_blank' rel='nofollow'>MDN</a> - [String.prototype.indexOf() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf)
- MSDN documentation: <a href='https://docs.microsoft.com/en-us/scripting/javascript/reference/indexof-method-string-javascript' target='_blank' rel='nofollow'>MSDN</a>