JavaScript String Methods Reference

Quick Reference for JavaScript String Methods

String Length (Property)

The length property returns the length of a string (number of characters).

// Example
let str = "Hello, world!";
console.log(str.length); // Output: 13

toUpperCase() Method

The toUpperCase() method returns the string converted to uppercase letters.

// Example
let str = "hello";
let upperStr = str.toUpperCase();
console.log(upperStr); // Output: HELLO

toLowerCase() Method

The toLowerCase() method returns the string converted to lowercase letters.

// Example
let str = "HELLO";
let lowerStr = str.toLowerCase();
console.log(lowerStr); // Output: hello

slice() Method

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

replace() Method

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!

includes() Method

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