10 JavaScript string methods you should know
Tags: javascript, beginners, webdev
Published: 2018-07-12 09:06:31

---
title: 10 JavaScript string methods you should know
published: true
description: 
tags: #JavaScript #newbies #webdev
---
This post is originally published to [my blog](http://frugencefidel.com/blog/10-javascript-string-methods-you-should-know).

In this post I will summarize 10 javascript string methods you should at least known according to me.

If interested read here [10 javascript array methods you should know
](http://frugencefidel.com/blog/10-javascript-array-methods-you-should-know).

String is anything inside single or double quotes.

Here are 10 javascript string methods you should know.

*1. startsWith()*

Check if string starts with specified character(s).

```javascript
  const str = 'JavaScript is amazing';

  console.log(str.startsWith('JavaScript')); // true
  console.log(str.startsWith('Java')); // true
  console.log(str.startsWith('javascript')); // false

  // position is optional
  // if not specified, the default value is 0
  console.log(str.startsWith('Script', 4)); // true
  console.log(str.startsWith('SCRIPT', 4)); // false
```

*2. endsWith()*

Check if string ends with specified character(s).

```javascript
  const str = 'JavaScript is amazing';

  // check if ends with 'amazing'
  console.log(str.endsWith('amazing')); // true
  console.log(str.endsWith('ing')); // true
  console.log(str.endsWith('Amazing')); // false

  // length is optional
  // if not specified, the default value is length of the string
  console.log(str.endsWith('is', 13)); // true
  console.log(str.endsWith('i', 13)); // false
  console.log(str.endsWith('s', 13)); // true
```

*3. includes()*

Check if string contains specified character(s).

```javascript
  const str = 'JavaScript is amazing';

  console.log(str.includes('Script')); // true
  console.log(str.includes('script')); // false
  console.log(str.includes(' ')); // true
  console.log(str.includes('array')); // false
```

*4. slice()*

Copy some part of string without modifying the original one.

```javascript
  const str = 'JavaScript is amazing';

  // Default start index is 0
  console.log(str.slice()); // 'JavaScript is amazing'

  // start copy at index 4
  console.log(str.slice(4)); // 'Script is amazing'

  // end copy at index 10(character at this index will not be included)
  console.log(str.slice(0, 10)); // 'JavaScript'
```

*5. toUpperCase()*

Convert string into upper case.

```javascript
  const str = 'JavaScript is amazing';

  console.log(str.toUpperCase()); // 'JAVASCRIPT IS AMAZING'
```

*6. toLowerCase()*

Convert string into lower case.

```javascript
  const str = 'JavaScript is amazing';

  console.log(str.toLowerCase()); // 'javascript is amazing'
```

*7. charAt()*

Return character at specified position.

```javascript
  const str = 'JavaScript is amazing';

  console.log(str.charAt()); // 'J'
  console.log(str.charAt(11)); // 'i'
  console.log(str.charAt(14)); // 'a'
  console.log(str.charAt(110)); // ''
```

*8. split()*

Split string into array of substrings.

```javascript
  const str = 'JavaScript is amazing';
  const strNew = 'JavaScript-is-amazing';

  console.log(str.split()); // ["JavaScript is amazing"]

  // Separator string used to determine where to make each split
  console.log(str.split('S')); // ["Java", "cript is amazing"]
  console.log(str.split('is')); // ["JavaScript ", " amazing"]
  console.log(str.split(' ')); // ["JavaScript", "is", "amazing"]
  console.log(strNew.split('-')); // ["JavaScript", "is", "amazing"]
```

*9. replace()*

Replaces specified value with another value in a string.

```javascript
  const str = 'JavaScript is amazing';

  console.log(str.replace('JavaScript', 'Node.js')); // 'Node.js is amazing'

  // replace() method is case sensitive
  // replace will not work
  console.log(str.replace('Javascript', 'Node.js')); // 'JavaScript is amazing'

  // use regular expression for case insensitive
  console.log(str.replace(/Javascript/i, 'Node.js')); // 'Node.js is amazing'

  // replace() method replaces only the first match
  console.log(str.replace('a', 'A')); // 'JAvaScript is amazing'

  // to replace all matches, use regular expression
  console.log(str.replace(/a/g, 'A')); // 'JAvAScript is AmAzing'
```

*10. repeat()*

Return new string with specified number of copies of existing string.

```javascript
  const str = 'JavaScript';

  console.log(str.repeat(3)); // 'JavaScriptJavaScriptJavaScript'
  console.log(str.repeat(1)); // 'JavaScript'
  console.log(str.repeat(0)); // ''
```