Master JavaScript Programming From Scratch

Clear, interactive, and structured JavaScript lessons designed for absolute beginners.

String Methods

JavaScript provides built-in methods that make it easier to work with and manipulate string values.

What are String Methods?

String methods are built-in JavaScript functions that help us perform common operations on strings.

We can use string methods to change the case of text, extract part of a string, replace text, search text, and perform many other operations.

Why Do We Use String Methods?

String methods make text manipulation easier, shorter, and easier to understand.

Common methods include toUpperCase(), toLowerCase(), slice(), and replace().

JavaScript String slice method

JavaScript String Methods - Example of the slice() method

Common String Methods

toUpperCase()
Convert to Uppercase

Converts all characters in a string into uppercase letters.

toLowerCase()
Convert to Lowercase

Converts all characters in a string into lowercase letters.

slice()
Extract String

Extracts a part of a string using start and end positions.

replace()
Replace Text

Replaces matching text with another value and returns a new string.

How String Methods Work

String Method Flow
"javascript"
→
toUpperCase()
→
"JAVASCRIPT"

The toUpperCase() method returns a new string with all letters converted to uppercase.

Example 1: toUpperCase()

The toUpperCase() method converts all characters of a string into uppercase.

let course = "JavaScript";

console.log(course.toUpperCase());
Output: JAVASCRIPT

Example 2: toLowerCase()

The toLowerCase() method converts all characters of a string into lowercase.

let message = "WELCOME TO CIIT";

console.log(message.toLowerCase());
Output: welcome to ciit

Example 3: slice()

The slice() method extracts a part of a string using starting and ending indexes.

let course = "JavaScript";

console.log(course.slice(0, 4));
Output: Java

Example 4: replace()

The replace() method replaces matching text with another value.

let message = "Learn Java";

let updatedMessage =
    message.replace("Java", "JavaScript");

console.log(updatedMessage);
Output: Learn JavaScript

Summary

JavaScript string methods provide ready-to-use operations for working with text. The toUpperCase() method converts text to uppercase, toLowerCase() converts text to lowercase, slice() extracts part of a string, and replace() replaces matching text.