Quick Reference for JavaScript String Methods
The length property returns the length of a string (number of characters).
// Example let str = "Hello, world!"; console.log(str.length); // Output: 13
The toUpperCase() method returns the string converted to uppercase letters.
// Example let str = "hello"; let upperStr = str.toUpperCase(); console.log(upperStr); // Output: HELLO
The toLowerCase() method returns the string converted to lowercase letters.
// Example let str = "HELLO"; let lowerStr = str.toLowerCase(); console.log(lowerStr); // Output: hello
The slice() method extracts a part of a string and returns it as a new string without modifying the original string.
// Example let str = "Hello, world!"; let slicedStr = str.slice(0, 5); console.log(slicedStr); // Output: Hello
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.
// Example
let str = "Hello, world!";
let newStr = str.replace("world", "JavaScript");
console.log(newStr); // Output: Hello, JavaScript!
The includes() method checks if a string contains a specified value and returns true or false.
// Example
let str = "Hello, world!";
let containsWorld = str.includes("world");
console.log(containsWorld); // Output: true