JavaScript String References

1. length

Returns the length of a string.

let str = "Hello, World!"; // The length property returns the number of characters in the string console.log(str.length); // Output: 13

2. toUpperCase()

Converts a string to uppercase letters.

let str = "Hello, World!"; // The toUpperCase() method converts all characters in the string to uppercase console.log(str.toUpperCase()); // Output: HELLO, WORLD!

3. includes()

Checks if a string contains a specified value.

let str = "Hello, World!"; // The includes() method checks if the string contains the specified substring console.log(str.includes("World")); // Output: true

4. slice()

Extracts a part of a string and returns it as a new string.

let str = "Hello, World!"; // The slice() method extracts a section of the string from index 0 to 5 console.log(str.slice(0, 5)); // Output: Hello

5. padStart()

Pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.

let str = "5"; // The padStart() method pads the string with "0" until the length is 3 console.log(str.padStart(3, "0")); // Output: 005

6. repeat()

Returns a new string with a specified number of copies of an existing string.

let str = "Hello"; // The repeat() method repeats the string 3 times console.log(str.repeat(3)); // Output: HelloHelloHello