Permalink
Please sign in to comment.
Showing
with
94 additions
and 0 deletions.
- +35 −0 js-String-prototype-concat.md
- +59 −0 js-String-prototype-indexOf.md
35
js-String-prototype-concat.md
| @@ -0,0 +1,35 @@ | ||
| +# String.prototype.concat() | ||
| + | ||
| +The concat() method combines the text of two or more strings and returns a new string. | ||
| + | ||
| +**Syntax** | ||
| +```js | ||
| +str.concat(string2, string3[,..., stringN]); | ||
| +``` | ||
| + | ||
| +### Parameters | ||
| + | ||
| +**string2...string*N*** | ||
| +The strings which are to be concatenated to this String. | ||
| + | ||
| +## Description | ||
| + | ||
| +The concat() method combines the text of two or more strings and returns the concatenated string. It does not modify the original strings. | ||
| + | ||
| +## Examples | ||
| + | ||
| +**Concatenating strings** | ||
| +```js | ||
| +var str1 = "Hello"; | ||
| +var str2 = "World"; | ||
| +console.log(str1.concat(str2)); | ||
| +// Console will output: HelloWorld | ||
| + | ||
| +var str2 = "Hello, "; | ||
| +console.log(str2.concat(" Welcome ", "to FCC.")); | ||
| +// Console will output: Hello, Welcome to FCC. | ||
| + | ||
| +``` | ||
| + | ||
| + | ||
| +Source [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) |
59
js-String-prototype-indexOf.md
| @@ -0,0 +1,59 @@ | ||
| +# String.prototype.indexOf() | ||
| + | ||
| +The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found. | ||
| + | ||
| +**Syntax** | ||
| +```js | ||
| +str.indexOf(searchValue[, *fromIndex*]); | ||
| +``` | ||
| + | ||
| +### Parameters | ||
| + | ||
| +**searchValue** | ||
| +A character/string whose index is to be found. | ||
| + | ||
| +**fromIndex** | ||
| +Optional. The location within the calling string to start the search from. It can be any integer. The default value is 0. If `fromIndex < 0` the entire string is searched (same as passing 0). If `fromIndex >= str.length`, the method will return -1 unless `searchValue` is an empty string in which case `str.length` is returned. | ||
| + | ||
| +## Description | ||
| + | ||
| +Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called `str` is `str.length - 1`. | ||
| + | ||
| +## Examples | ||
| + | ||
| +**Finding a character in a string** | ||
| +```js | ||
| +var str1 = "Hello"; | ||
| +console.log(str1.indexOf('H')); | ||
| +// Console will output: 0 | ||
| + | ||
| +var str1 = "Hello"; | ||
| +console.log(str1.indexOf('Y')); | ||
| +// Console will output: -1. | ||
| + | ||
| +``` | ||
| + | ||
| +**indexOf() is case-sensitive** | ||
| +```js | ||
| +var str1 = "Hello"; | ||
| +console.log(str1.indexOf('ello')); | ||
| +// Console will output 1 | ||
| + | ||
| +var str1 = "Hello"; | ||
| +console.log(str1.indexOf('Ello')); | ||
| +// Console will output -1 | ||
| + | ||
| +``` | ||
| + | ||
| +**indexOf() with fromIndex** | ||
| +```js | ||
| +var str1 = "FreeCodeCamp is a place for people to learn"; | ||
| +console.log(str1.indexOf('Camp')); | ||
| +// Console will output 8 | ||
| + | ||
| +console.log(str1.indexOf('Camp', 9)); | ||
| +// Console will output -1 | ||
| + | ||
| +``` | ||
| + | ||
| +Source [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) |
0 comments on commit
f13ccff