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 Methods - Example of the slice() method
Common String Methods
Convert to Uppercase
Converts all characters in a string into uppercase letters.
Convert to Lowercase
Converts all characters in a string into lowercase letters.
Extract String
Extracts a part of a string using start and end positions.
Replace Text
Replaces matching text with another value and returns a new string.
How String Methods Work
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());
Example 2: toLowerCase()
The toLowerCase() method converts all characters of a string into lowercase.
let message = "WELCOME TO CIIT";
console.log(message.toLowerCase());
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));
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);
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.