Methods for String Manipulation in JavaScript

JavaScript provides a variety of methods to work with strings efficiently. Strings are a fundamental data type in JavaScript and are often used to store and manipulate text. This chapter will explain all the key string manipulation methods from basic to advanced, providing examples, detailed explanations, and insights so that you do not need any other resource to understand these concepts.

Understanding Strings in JavaScript

  • Before diving into string methods, it is important to understand what strings are in JavaScript.
  • Definition: A string is a sequence of characters enclosed in quotes.

  • Strings can be created using single quotes ('), double quotes ("), or backticks (`).

Example:

				
					console.log("helloword")let singleQuote = 'Hello, World!';  // Single quotes
let doubleQuote = "Hello, JavaScript!";  // Double quotes
let templateLiteral = `Hello, Developer!`;  // Backticks (Template Literal)
console.log(singleQuote, doubleQuote, templateLiteral);
				
			

Output

				
					Hello, World! Hello, JavaScript! Hello, Developer!
				
			

Strings are immutable: You cannot change a character directly in a string. You can, however, create a new string with modifications.

Basic String Methods

These are the most commonly used string manipulation methods in JavaScript.

length Property

The length property returns the number of characters in a string.

Example:

				
					let str = "JavaScript";
console.log(str.length);  // Output: 10
				
			

charAt(index)

The charAt method returns the character at a specified position (index) in a string.

Example:

				
					let str = "Hello";
console.log(str.charAt(1));  // Output: e (Index starts from 0)
				
			

charCodeAt(index)

The charCodeAt method returns the Unicode value of the character at the specified position.

Example:

				
					let str = "A";
console.log(str.charCodeAt(0));  // Output: 65 (Unicode value of 'A')
				
			

toUpperCase() and toLowerCase()

  • toUpperCase() converts a string to uppercase.

  • toLowerCase() converts a string to lowercase.

Example:

				
					let str = "JavaScript";
console.log(str.toUpperCase());  // Output: JAVASCRIPT
console.log(str.toLowerCase());  // Output: javascript
				
			

trim()

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

Example:

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

Extracting Parts of Strings

JavaScript provides methods to extract parts of strings: slice, substring, and substr.

slice(start, end)

The slice method extracts a section of a string and returns it as a new string.

  • start: The index to start extraction.

  • end: The index to stop extraction (not included).

Example:

				
					let str = "Hello, JavaScript!";
console.log(str.slice(7, 17));  // Output: JavaScript
				
			

substring(start, end)

The substring method works similarly to slice but does not accept negative indexes.

Example:

				
					let str = "Hello, JavaScript!";
console.log(str.substring(7, 17));  // Output: JavaScript
				
			

Note: This method is deprecated and should not be used in modern JavaScript.

Searching in Strings

To search for specific substrings or patterns in a string, use the following methods:

indexOf()

The indexOf method returns the position of the first occurrence of a substring. If not found, it returns -1.

Example:

				
					let str = "Hello, World!";
console.log(str.indexOf("World"));  // Output: 7
console.log(str.indexOf("JavaScript"));  // Output: -1
				
			

lastIndexOf()

The lastIndexOf method returns the position of the last occurrence of a substring.

Example:

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

includes()

The includes method checks if a string contains a specific substring and returns true or false.

Example:

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

startsWith() and endsWith()

  • startsWith() checks if a string starts with a specified substring.

  • endsWith() checks if a string ends with a specified substring.

Example:

				
					let str = "Hello, JavaScript!";
console.log(str.startsWith("Hello"));  // Output: true
console.log(str.endsWith("!"));  // Output: true
				
			

Replacing Content in Strings

replace()

The replace method replaces the first occurrence of a substring with a new string.

Example:

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

replaceAll()

The replaceAll method replaces all occurrences of a substring.

Example:

				
					let str = "Hello, World! World is big!";
let newStr = str.replaceAll("World", "JavaScript");
console.log(newStr);  // Output: Hello, JavaScript! JavaScript is big!
				
			

Splitting and Joining Strings

split()

The split method splits a string into an array of substrings based on a delimiter.

Example:

				
					let str = "Hello,World,JavaScript";
let arr = str.split(",");
console.log(arr);  // Output: ["Hello", "World", "JavaScript"]
				
			

join()

The join method joins an array into a string with a specified separator.

Example:

				
					let arr = ["Hello", "World", "JavaScript"];
let str = arr.join(" ");
console.log(str);  // Output: Hello World JavaScript
				
			

String manipulation in JavaScript is essential for working with text-based data. Methods such as slice, replace, split, and join provide powerful tools for handling strings efficiently. By mastering these methods, you can perform complex text operations with ease and precision. This chapter covered everything from basic concepts to advanced techniques, ensuring you have a complete understanding of string manipulation. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India