JavaScript String Methods

What are JavaScript String Methods?

JavaScript String Methods are built-in functions that help in manipulating and modifying strings. These methods allow developers to perform tasks such as finding the length of a string, changing the case of characters, searching for specific substrings, and much more. These methods are essential for handling textual data in web development.

Basic String Methods

Understanding basic string methods is key when working with strings in JavaScript. These methods are commonly used for extracting substrings, converting case, and measuring string length. Mastering these methods is important for any developer.

.length

The .length property is used to find the number of characters in a string. It does not require parentheses because it is a property, not a function.

Example:

let str = "hello";
console.log(str.length); // Output: 5

.charAt()

The .charAt() method retrieves the character at a specific index within a string. JavaScript uses zero-based indexing, meaning the first character is at index 0.

Example:

let myString = 'jQuery FTW!!!';
console.log(myString.charAt(7)); // Output: "F"

.charCodeAt()

The .charCodeAt() method returns the Unicode value of the character at a specified index.

Example:

let str = "hello";
console.log(str.charCodeAt(1)); // Output: 101

.concat()

The .concat() method joins two or more strings together.

Example:

let string1 = "Hello";
let string2 = "World";
let combinedString = string1.concat(" ", string2);
console.log(combinedString); // Output: "Hello World"

.indexOf()

The .indexOf() method returns the index of the first occurrence of a specified substring. If the substring is not found, it returns -1.

Example:

let str = "Hello, world!";
console.log(str.indexOf("world")); // Output: 7

.lastIndexOf()

The .lastIndexOf() method finds the last occurrence of a character or substring within a string.

Example:

let str = "Hello, world!";
console.log(str.lastIndexOf("o")); // Output: 8

String Manipulation Methods

These methods allow developers to modify strings by converting case, trimming spaces, and more.

.toLowerCase()

The .toLowerCase() method converts all characters in a string to lowercase.

Example:

let originalString = "Hello World";
console.log(originalString.toLowerCase()); // Output: "hello world"

.toUpperCase()

The .toUpperCase() method converts all characters in a string to uppercase.

Example:

let originalString = "hello world";
console.log(originalString.toUpperCase()); // Output: "HELLO WORLD"

.trim()

The .trim() method removes whitespace from both ends of a string.

Example:

let str = "  Hello, World!  ";
console.log(str.trim()); // Output: "Hello, World!"

Searching and Replacing Methods

These methods help in searching for substrings and replacing parts of a string.

.includes()

The .includes() method checks if a string contains a specified substring and returns true or false.

Example:

let str = "Hello, world!";
console.log(str.includes("world")); // Output: true

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate