Skip to content

Commit

Permalink
Merge pull request #14 from X-yn/patch-1
Browse files Browse the repository at this point in the history
String.prototype.length is not a method
  • Loading branch information
AlbertoMontalesi authored Jul 9, 2019
2 parents 0d9c81a + f972935 commit 07de969
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions ebook/05_additional_string_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,40 @@

There are many methods that we can use against strings. Here's a list of a few of them:

```js
// .length()
const str = 'hello'
str.length() // 5
// .indexOf()
const str2 = 'this is a short sentence'
str2.indexOf('short'); // 10
// slice()
const str3 = 'pizza, orange, cereals'
str3.slice(0,5) // 'pizza'
// toUppercase() / toLowerCase()
const str4 = 'lowercase'
str4.toUpperCase(); // 'LOWERCASE'
const str5 = 'UPPERCASE'
str5.toLowerCase() // 'uppercase'
```

There are many more methods, these were just a few as a reminder.
1. **indexOf()**

Gets the position of the first occurence of the specified value in a string.
```js
const str = "this is a short sentence";
str.indexOf("short");
// Output: 10
```
2. **slice()**

Pulls a specified part of a string as a new string.
```js
const str = "pizza, orange, cereals"
str.slice(0, 5);
// Output: "pizza"
```
3. **toUpperCase()**

Turns all characters of a string to uppercase.
```js
const str = "i ate an apple"
str.toUpperCase()
// Output: "I ATE AN APPLE"
```
4. **toLowerCase()**

Turns all characters of a string to lowercase.
```
const str = "I ATE AN APPLE"
str.toLowerCase()
// Output: "i ate an apple"
```

There are many more methods, these were just a few as a reminder. Check the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#) for a more in-depth description on the above methods.

 

Expand Down Expand Up @@ -112,4 +128,4 @@ As the name suggests, this new method will repeat what we pass in.
let hello = "Hi";
console.log(hello.repeat(10));
// "HiHiHiHiHiHiHiHiHiHi"
```
```

0 comments on commit 07de969

Please sign in to comment.