JavaScript String Methods

JavaScript offers a wide range of string methods to manipulate and handle strings effectively.

JavaScript String Methods

  • charAt(): Returns character at a specified index.
  • concat(): Joins two or more strings.
  • includes(): Checks if a string contains a specified substring.
  • indexOf(): Finds the first occurrence of a value.
  • slice(), substring(), substr(): Extract parts of a string.
  • replace(): Replaces a value with another value.
  • toLowerCase(), toUpperCase(): Changes the case of a string.
  • trim(): Removes whitespace.
  • split(): Splits a string into an array.
  • repeat(): Repeats the string a specified number of times.
  • match(), search(): Work with regular expressions.

 

1. charAt()

Returns the character at a specified index in a string.

let text = 'JavaScript';
console.log(text.charAt(0));  // Output: J

Try It Now

2. concat()

Concatenates (joins) two or more strings and returns a new string.

let text1 = 'Hello';
let text2 = 'World';
console.log(text1.concat(' ', text2));  // Output: Hello World

Try It Now

3. includes()

Checks if a string contains a specified substring, returning true or false.

let text = 'Hello, World!';
console.log(text.includes('World'));  // Output: true

Try It Now

4. indexOf()

Returns the index of the first occurrence of a specified value in a string. If not found, it returns -1.

let text = 'Hello, World!';
console.log(text.indexOf('World'));  // Output: 7

Try It Now

5. lastIndexOf()

Returns the index of the last occurrence of a specified value in a string.

let text = 'Hello, World! World!';
console.log(text.lastIndexOf('World'));  // Output: 14

Try It Now

6. slice()

Extracts a section of a string and returns it as a new string, without modifying the original string.

let text = 'Hello, World!';
console.log(text.slice(7, 12));  // Output: World

Try It Now

7. substring()

Returns a portion of the string between two specified indices.

let text = 'Hello, World!';
console.log(text.substring(7, 12));  // Output: World

Try It Now

8. substr()

Returns a part of the string, starting at a specified index and extending for a given number of characters.

let text = 'Hello, World!';
console.log(text.substr(7, 5));  // Output: World

Try It Now

9. replace()

Replaces a specified value with another value in a string.

let text = 'Hello, World!';
console.log(text.replace('World', 'JavaScript'));  // Output: Hello, JavaScript!

Try It Now

10. toLowerCase() / toUpperCase()

Converts a string to lowercase or uppercase.

let text = 'JavaScript';
console.log(text.toLowerCase());  // Output: javascript
console.log(text.toUpperCase());  // Output: JAVASCRIPT

Try It Now

11. trim()

Removes whitespace from both ends of a string.

let text = '  Hello, World!  ';
console.log(text.trim());  // Output: 'Hello, World!'

Try It Now

12. split()

Splits a string into an array of substrings, based on a specified delimiter.

let text = 'Apple, Banana, Cherry';
let fruits = text.split(', ');
console.log(fruits);  // Output: ['Apple', 'Banana', 'Cherry']

Try It Now

13. repeat()

Returns a new string with a specified number of copies of the original string.

let text = 'Hello!';
console.log(text.repeat(3));  // Output: Hello!Hello!Hello!

Try It Now

14. match()

Retrieves the matches when matching a string against a regular expression.

let text = 'The rain in Spain';
console.log(text.match(/ain/g));  // Output: ['ain', 'ain']

Try It Now

15. search()

Searches a string for a specified value and returns the position of the match.

let text = 'The rain in Spain';
console.log(text.search('rain'));  // Output: 4

Try It Now

Summary

These methods are essential for text manipulation and are widely used in various JavaScript applications.