JS String Methods
In this page:
string.length;
string.slice(start, end);
string.replace(old, new);
string.split(separator);
string.toUpperCase();
Changing Case
toUpperCase() converts every letter in a string to uppercase, and toLowerCase() converts every letter to lowercase, both returning a brand new string rather than changing the original.
उदाहरण: Changing Case
// Declare the constant `text`, set to "Hello World"
// Declare the constant `text`, set to "Hello World"
const text = "Hello World";
// Print `text.toUpperCase()` to the console
// Print `text.toUpperCase()` to the console
console.log(text.toUpperCase());
// Print `text.toLowerCase()` to the console
// Print `text.toLowerCase()` to the console
console.log(text.toLowerCase());
trim
trim() removes whitespace from the beginning and end of a string, which is especially useful for cleaning up user-typed input that might contain accidental leading or trailing spaces.
उदाहरण: trim
const input = " hello ";
console.log(`"${input.trim()}"`);
slice and substring
slice(start, end) and substring(start, end) both extract a portion of a string based on index positions, slice supports negative indexes counting from the end, while substring treats negative values as zero.
उदाहरण: slice and substring
// Declare the constant `text`, set to "JavaScript"
// Declare the constant `text`, set to "JavaScript"
const text = "JavaScript";
// Print `text.slice(0, 4)` to the console
// Print `text.slice(0, 4)` to the console
console.log(text.slice(0, 4));
// Print `text.slice(-6)` to the console
// Print `text.slice(-6)` to the console
console.log(text.slice(-6));
// Print `text.substring(0, 4)` to the console
// Print `text.substring(0, 4)` to the console
console.log(text.substring(0, 4));
replace and replaceAll
replace() swaps the first match of a specified text or pattern with new text, while replaceAll() swaps every matching occurrence throughout the entire string.
उदाहरण: replace and replaceAll
// Declare the constant `text`, set to "cat cat cat"
// Declare the constant `text`, set to "cat cat cat"
const text = "cat cat cat";
// Print `text.replace("cat", "dog")` to the console
// Print `text.replace("cat", "dog")` to the console
console.log(text.replace("cat", "dog"));
// Print `text.replaceAll("cat", "dog")` to the console
// Print `text.replaceAll("cat", "dog")` to the console
console.log(text.replaceAll("cat", "dog"));
split, includes, startsWith, and endsWith
split() breaks a string into an array of pieces based on a separator, while includes(), startsWith(), and endsWith() each check for the presence of specific text at any position, the start, or the end of a string.
उदाहरण: split, includes, startsWith, and endsWith
// Declare the constant `tags`, set to "html,css,js"
// Declare the constant `tags`, set to "html,css,js"
const tags = "html,css,js";
// Print `tags.split(",")` to the console
// Print `tags.split(",")` to the console
console.log(tags.split(","));
// Print `tags.includes("css")` to the console
// Print `tags.includes("css")` to the console
console.log(tags.includes("css"));
// Print `tags.startsWith("html")` to the console
// Print `tags.startsWith("html")` to the console
console.log(tags.startsWith("html"));
// Print `tags.endsWith("js")` to the console
// Print `tags.endsWith("js")` to the console
console.log(tags.endsWith("js"));
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: