Boost Your JavaScript Skills with These Essential String Methods

Ashwini Paraye
3 min readMay 16, 2023

JavaScript is a programming language commonly used for creating interactive web pages. One of the fundamental data types in JavaScript is a string, which represents text. A string is a sequence of characters enclosed in single quotes (‘ ‘) or double quotes (“ “).

String methods are functions that can be called on a string to perform various operations such as finding a substring, converting the case, trimming whitespace, etc. In this article, we’ll discuss some of the most commonly used string methods in JavaScript. Let’s get started!

1. length :

The length property is a built-in property of string objects. It is used to find the length of a string, which is the number of characters in the string. The length property is a read-only property, which means that it cannot be modified.

Example :

let myString = "Hello World";
let stringLength = myString.length;

console.log(stringLength); // Output: 11

2. charAt(index) :

This method returns the character at a specified index in a string.

let myString = "Hello";
let result = myString.charAt(1);

console.log(result); // Output: "e"

3. concat(str1, str2, …) :

This method joins two or more strings and returns a new string.

let firstName = "John";
let lastName = "Doe";
let fullName = firstName.concat(" ", lastName);

console.log(fullName); // Output: "John Doe"

4. includes(searchValue, startIndex) :

This method checks whether a string contains a specified substring and returns a boolean value.

let myString = "Hello World";
let result = myString.includes("World");

console.log(result); // Output: true

5. indexOf(searchValue, startIndex) :

This method returns the index of the first occurrence of a specified substring in a string.

let myString = "Hello World";
let result = myString.indexOf("World");

console.log(result); // Output: 6

6. lastIndexOf(searchValue, startIndex) :

This method returns the index of the last occurrence of a specified substring in a string.

let myString = "Hello World";
let result = myString.lastIndexOf("o");

console.log(result); // Output: 7

7. match() :

The match() method is used to search a string for a regular expression and return the matches.

let myString = "The quick brown fox jumps over the lazy dog.";
console.log(myString.match(/o/g)); // Output: ["o", "o", "o"]

8. replace(searchValue, replaceValue) :

This method replaces a specified substring with another substring.

let myString = "Hello World";
let result = myString.replace("World", "Universe");

console.log(result); // Output: "Hello Universe"

9. slice(startIndex, endIndex) :

This method extracts a section of a string and returns a new string.

let myString = "Hello World";
let result = myString.slice(6, 11);

console.log(result); // Output: "World"

10. split(separator, limit) :

This method splits a string into an array of substrings based on a specified separator.

let myString = "Hello,World";
let result = myString.split(",");

console.log(result); // Output: ["Hello", "World"]

11. substr(startIndex, length) :

The substr() method takes two parameters. The startIndex parameter specifies the starting index from which the substring should be extracted, and the length parameter determines the number of characters to include in the substring. Returns a new string.

Indexing start from zero (0).

let myString = "Hello World";
let result = myString.substr(6, 5);

console.log(result); // Output: "World"

12. toLowerCase() :

This method converts a string to lowercase.

let myString = "HELLO WORLD";
let result = myString.toLowerCase();

console.log(result); // Output: "hello world"

13. toUpperCase() :

This method converts a string to uppercase.

let myString = "hello world";
let result = myString.toUpperCase();

console.log(result); // Output: "HELLO WORLD"

14. trim() :

This method removes whitespace from both ends of a string.

let myString = "    Hello World    ";
let result = myString.trim();

console.log(result); // Output: "Hello World"

15. charCodeAt() :

The charCodeAt() method is used to return the Unicode value of the character at a specific index of a string.

let myString = "Hello World!";
console.log(myString.charCodeAt(1)); // Output: 101

16. substring(startIndex, endIndex) :

The substring() method also takes two parameters. The startIndex parameter specifies the starting index from which the substring should be extracted, and the endIndex parameter specifies the index up to which the substring should be extracted. The character at the endIndex position is not included in the extracted substring. Returns a new string.

let myString = "Hello, World!";

console.log(myString.substring(7, 12)); // Output: "World"

These are the most commonly used string methods in JavaScript. By utilizing them effectively, you can manipulate strings to suit your application’s needs.

I hope you found this article helpful. Thank you for reading! 😀

Happy Coding !

--

--

Ashwini Paraye

Fullstack Developer | 👨‍💻 Writing about programming concepts | React.js | JavaScript | Vue.js | Golang