JavaScript JunkiesJavaScript Junkies Unleash Your Coding Superpowers with JavaScript Junkies

JavaScript String Methods (With Examples)

JavaScript provides a wide range of methods for manipulating strings. These methods allow you to perform a variety of tasks, including extracting parts of a string, searching for a specific string, replacing parts of a string, and much more. In this article, we’ll cover some of the most commonly used string methods in JavaScript.

2. The charAt() Method

The charAt() method returns the character at a specified index in a string. The index is specified as an integer between 0 and the length of the string minus 1.

let myString = "Hello, world!";
let firstChar = myString.charAt(0);
console.log(firstChar); // Output: "H"

3. The charCodeAt() Method

The charCodeAt() method returns the Unicode value of the character at a specified index in a string. The index is specified as an integer between 0 and the length of the string minus 1.

let myString = "Hello, world!";
let firstCharCode = myString.charCodeAt(0);
console.log(firstCharCode); // Output: 72

4. The concat() Method

The concat() method joins two or more strings and returns a new string. This method does not change the existing strings, but instead returns a new string that contains the concatenated text.

let firstName = "John";
let lastName = "Doe";
let fullName = firstName.concat(" ", lastName);
console.log(fullName); // Output: "John Doe"

5. The endsWith() Method

The endsWith() method checks whether a string ends with a specified substring. It returns true if the string ends with the specified substring, and false otherwise.

let myString = "Hello, world!";
let endsWithWorld = myString.endsWith("world!");
console.log(endsWithWorld); // Output: true

6. The includes() Method

The includes() method checks whether a string contains a specified substring. It returns true if the string contains the specified substring, and false otherwise.

let myString = "Hello, world!";
let includesWorld = myString.includes("world");
console.log(includesWorld); // Output: true

7. The indexOf() Method

The indexOf() method returns the index of the first occurrence of a specified substring in a string. If the substring is not found, it returns -1.

let myString = "Hello, world!";
let includesWorld = myString.includes("world");
console.log(includesWorld); // Output: true

8. The lastIndexOf() Method

The lastIndexOf() method returns the index of the last occurrence of a specified substring in a string. If the substring is not found, it returns -1.

let myString = "Hello, world!";
let lastIndexOfL = myString.lastIndexOf("l");
console.log(lastIndexOfL); // Output: 10

9. The match() Method

The match() method searches a string for a specified pattern and returns an array of the matches. The pattern can be a regular expression or a string.

let myString = "Hello, world!";
let matches = myString.match(/[lo]/g);
console.log(matches); // Output: ["l", "l", "o"]

10. The repeat() Method

The repeat() method returns a new string with a specified number of copies of the original string concatenated together.

let myString = "Hello!";
let repeatedString = myString.repeat(3);
console.log(repeatedString); // Output: "Hello!Hello!Hello!"

11. The replace() Method

The replace() method searches a string for a specified substring or regular expression and replaces it with a new string. The original string is not modified by this method.

let myString = "Hello, world!";
let newString = myString.replace("world", "JavaScript");
console.log(newString); // Output: "Hello, JavaScript!"

12. The search() Method

The search() method searches a string for a specified substring or regular expression and returns the index of the first match. If no match is found, it returns -1.

let myString = "Hello, world!";
let index = myString.search(/o/);
console.log(index); // Output: 4

13. The slice() Method

The slice() method extracts a section of a string and returns it as a new string. The section is specified by a starting index and an optional ending index.

let myString = "Hello, world!";
let newString = myString.slice(7, 12);
console.log(newString); // Output: "world"

14. The split() Method

The split() method splits a string into an array of substrings based on a specified separator. The separator can be a string or a regular expression.

let myString = "apple,banana,orange";
let fruits = myString.split(",");
console.log(fruits); // Output: ["apple", "banana", "orange"]

15. The startsWith() Method

The startsWith() method checks whether a string starts with a specified substring. It returns true if the string starts with the specified substring, and false otherwise.

let myString = "Hello, world!";
let startsWithHello = myString.startsWith("Hello");
console.log(startsWithHello); // Output: true

16. The substr() Method

The substr() method extracts a specified number of characters from a string, starting at a specified index.

let myString = "Hello, world!";
let newString = myString.substr(7, 5);
console.log(newString); // Output: "world"

17. The substring() Method

The substring() method extracts the characters between two specified indexes in a string.

let myString = "Hello, world!";
let newString = myString.substring(7, 12);
console.log(newString); // Output: "world"

18. The toLocaleLowerCase() Method

The toLocaleLowerCase() method converts a string to lowercase letters according to the host’s current locale.

let myString = "HELLO WORLD";
let newString = myString.toLocaleLowerCase();
console.log(newString); // Output: "hello world"

19. The toLocaleUpperCase() Method

The toLocaleUpperCase() method converts a string to uppercase letters according to the host’s current locale.

let myString = "hello world";
let newString = myString.toLocaleUpperCase();
console.log(newString); // Output: "HELLO WORLD"

20. The toLowerCase() Method

The toLowerCase() method converts a string to lowercase letters.

let myString = "HELLO WORLD";
let newString = myString.toLowerCase();
console.log(newString); // Output: "hello world"

21. The toString() Method

The toString() method returns a string representing the specified object.

The toUpperCase() method converts a string to uppercase letters.

let myNumber = 123;
let newString = myNumber.toString();
console.log(typeof newString); // Output: "string"

22. The toUpperCase() Method

let myString = "hello world";
let newString = myString.toUpperCase();
console.log(newString); // Output: "HELLO WORLD"

23. The trim() Method

The trim() method removes whitespace from both ends of a string.

let myString = "   hello world   ";
let newString = myString.trim();
console.log(newString); // Output: "hello world"

24. Conclusion

In conclusion, JavaScript provides a wide range of powerful string manipulation methods that can be used to perform a variety of tasks. Whether you’re extracting parts of a string, searching for a specific substring, or replacing parts of a string, there’s a method available to help you get the job done.

So go ahead and experiment with these methods to see what you can accomplish with JavaScript strings!

FAQs

What is a string in JavaScript?

A string in JavaScript is a sequence of characters enclosed in quotation marks.

What is the difference between the indexOf() and lastIndexOf() methods?

The indexOf() method returns the index of the first occurrence of a specified substring, while the lastIndexOf() method returns the index of the last occurrence.

Can the replace() method replace all occurrences of a substring in a string?

Yes, the replace() method can replace all occurrences of a substring by using a regular expression with the g (global) flag.

What is a regular expression?

A regular expression is a pattern used to match character combinations in strings.

Are JavaScript string methods case-sensitive?

Yes, JavaScript string methods are case-sensitive.

Press ESC to close