String Methods

Javascript 7 min min read Updated: Mar 09, 2026 Beginner
String Methods
Beginner Topic 12 of 15

String Methods in JavaScript

Strings are used in JavaScript to represent textual data such as names, messages, or sentences. JavaScript provides many built-in string methods that allow developers to manipulate and work with text easily.

String methods help perform operations such as finding characters, converting text to uppercase or lowercase, extracting parts of a string, and replacing text.

What is a String Method?

A string method is a built-in function that performs operations on strings. These methods allow developers to modify or analyze string data without writing complex logic.

Example of a string:

javascript let text = "JavaScript is powerful";

1. length Property

The length property returns the total number of characters in a string.

javascript let text = "JavaScript"; console.log(text.length);

Output:

output 10

2. toUpperCase()

The toUpperCase() method converts all characters in a string to uppercase.

javascript let text = "hello"; console.log(text.toUpperCase());

Output:

output HELLO

3. toLowerCase()

The toLowerCase() method converts all characters in a string to lowercase.

javascript let text = "HELLO"; console.log(text.toLowerCase());

Output:

output hello

4. indexOf()

The indexOf() method returns the position of the first occurrence of a specified value in a string.

javascript let text = "JavaScript"; console.log(text.indexOf("S"));

5. slice()

The slice() method extracts a portion of a string and returns it as a new string.

javascript let text = "JavaScript"; console.log(text.slice(0, 4));

Output:

output Java

6. replace()

The replace() method replaces a specified value with another value in a string.

javascript let text = "I like Java"; let newText = text.replace("Java", "JavaScript"); console.log(newText);

Output:

output I like JavaScript

7. trim()

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

javascript let text = " Hello World "; console.log(text.trim());

8. split()

The split() method splits a string into an array based on a specified separator.

javascript let text = "apple,banana,mango"; let fruits = text.split(","); console.log(fruits);

Output:

output ["apple", "banana", "mango"]

9. includes()

The includes() method checks whether a string contains a specified value. It returns true or false.

javascript let text = "JavaScript Tutorial"; console.log(text.includes("Script"));

Output:

output true

Why String Methods are Important

String methods make it easier to work with text data in JavaScript programs. They are commonly used in web development for tasks such as form validation, searching text, formatting data, and processing user input.

Understanding string methods helps developers write cleaner and more efficient code.

Conclusion

JavaScript provides many powerful string methods that allow developers to manipulate and analyze textual data easily. Methods like toUpperCase(), slice(), replace(), and split() are widely used in everyday programming.

By mastering these methods, developers can efficiently handle string operations in JavaScript applications.

In the next tutorial, you will learn about Array Methods in JavaScript, which help manipulate collections of data.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators