From 9fe78e3827f10f1c3a9a6e592a216f0a8e14f241 Mon Sep 17 00:00:00 2001 From: Hasan Abdullah <37593075+HasanAbdullah31@users.noreply.github.com> Date: Sat, 6 Jul 2019 12:08:33 -0400 Subject: [PATCH] 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> --- .../string/string-prototype-indexof/index.md | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/guide/english/javascript/standard-objects/string/string-prototype-indexof/index.md b/guide/english/javascript/standard-objects/string/string-prototype-indexof/index.md index 813d8358b7..4104b17c77 100644 --- a/guide/english/javascript/standard-objects/string/string-prototype-indexof/index.md +++ b/guide/english/javascript/standard-objects/string/string-prototype-indexof/index.md @@ -2,7 +2,8 @@ title: 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** ```javascript @@ -10,17 +11,25 @@ str.indexOf(searchValue[, fromIndex]) ``` ### 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 -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 ```javascript 'Blue Whale'.indexOf('Blue'); // returns 0 'Blue Whale'.indexOf('Blute'); // returns -1 +'Blue Whale'.indexOf('blue'); // returns -1 'Blue Whale'.indexOf('Whale', 0); // returns 5 'Blue Whale'.indexOf('Whale', 5); // returns 5 '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('', 10); // returns 10 'Blue Whale'.indexOf('', 11); // returns 10 -'Blue Whale'.indexOf('blue'); // returns -1 ``` ### More Information: -- MDN documentation: MDN -- MSDN documentation: MSDN +- [String.prototype.indexOf() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf)