DEV Community

Cover image for πŸš€ Master the Most Powerful JavaScript String Methods (With Real-World Examples)
Tanvir Ahmed
Tanvir Ahmed

Posted on β€’ Edited on

πŸš€ Master the Most Powerful JavaScript String Methods (With Real-World Examples)

Key Features of JavaScript String Methods

βœ… Immutability: Strings in JavaScript are immutable, meaning their original value cannot be changed. String methods return a new string or value without altering the original string.

βœ… Case Sensitivity: String methods are case-sensitive. For example, "Hello" and "hello" are treated as different strings.

βœ… Chaining Methods: String methods can be chained together to perform multiple operations in a single line of code.

βœ… Regular Expressions: Many string methods, such as match(), replace(), and split(), support regular expressions for advanced pattern matching and manipulation.


Now List String Method

No Method
01 toUpperCase()
02 toLowerCase()
03 trim()
04 charAt()
05 split()
06 concat()
07 slice()
08 replace()
09 include()
10 repeat()
11 indexOf()
12 startWith() & endsWith()

1.toUpperCase():

The toUpperCase() method is used to convert all the alphabetic characters in a string to uppercase. Non-alphabetic characters, such as numbers, spaces, or symbols, remain unchanged.

syntax: string.toUpperCase();

Image description

2.toLowerCase():

The toLowerCase() method is used to convert all the alphabetic characters in a string to lowercase. Like toUpperCase(), non-alphabetic characters remain unchanged.

syntax: string.toLowerCase();

Image description

3.trim():

The trim() method in JavaScript is used to remove whitespace from both the beginning and the end of a string. Whitespace characters include spaces, tabs, and newline characters.

syntax: string.trim;

Image description

4.charAt():

The charAt() method in JavaScript is used to retrieve a character at a specific index in a string. It returns the character at the given position, or an empty string if the index is out of range.

syntax: string.charAt();

Image description

5.split():

The split() method in JavaScript is used to divide a string into an array of substrings based on a specified separator. It splits the string wherever the separator occurs and returns a new array with the resulting substrings.

syntax: string.split([separator[, limit]]);

Image description

6.concat():

The concat() method in JavaScript is used to merge two or more arrays or strings into a new array or string. It does not modify the original array or string, but instead returns a new one that contains the merged data.

string1.concat(string2, string3, ..., stringN);

Image description

7.slice():

The slice() method in JavaScript extracts a section of an array or string and returns it as a new array or string, without modifying the original array or string.

string.slice(start, end);

Image description

8.replace():

The replace() method in JavaScript allows you to replace parts of a string with another string. It works with both strings and regular expressions.

string.replace(pattern, replacement)

Image description

9.include():

The includes() method determines whether a string contains a specified substring, returning true or false based on the presence of the substring.

Features
i.Case-Sensitive: The includes() method is case-sensitive.
ii.Boolean Result: Returns a true or false.

string.includes(searchString, position)

Image description

10.repeat():

The repeat() method constructs and returns a new string, consisting of the specified number of copies of the original string, concatenated together.

string.repeat(count)

Image description

11.indexOf():

The indexOf() method in JavaScript is used to determine the first occurrence of a specified substring within a string. It returns the index of the first occurrence of the substring. If the substring is not found, it returns -1.

Return Value:
The index of the first occurrence of searchValue.-1 if searchValue is not found.

string.indexOf(searchValue, fromIndex)

Image description

12.startWith() & endsWith():

The startsWith() and endsWith() methods in JavaScript are used to check whether a string begins or ends with a specified substring. Both methods return a boolean (true or false).

string.startsWith(searchString, position)
string.endsWith(searchString, length)

Image description

Top comments (0)