You are currently viewing JavaScript Cheat Sheet 10.15 : JavaScript String Methods

JavaScript Cheat Sheet 10.15 : JavaScript String Methods

Mastering JavaScript String Methods

Welcome to the world of programming with JavaScript! In this tutorial, we will delve into the fundamentals of JavaScript strings and explore various string methods that will empower you to manipulate and work with text effectively.

Section 1: Understanding JavaScript Strings

1.1 What are Strings?

Strings in JavaScript are sequences of characters, enclosed within single (‘ ‘) or double (” “) quotes. They can represent text and are a fundamental data type in the language.

// Sample String
let greeting = "Hello, World!";

Section 2: Common JavaScript String Methods

2.1 Length:

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

let message = "Learning JavaScript String Methods";
console.log(message.length); // Output: 31

2.2 Concatenation:

The concat() method combines two or more strings.

let firstName = "John";
let lastName = "Doe";
let fullName = firstName.concat(" ", lastName);
console.log(fullName); // Output: John Doe

2.3 Uppercase and Lowercase:

The toUpperCase() and toLowerCase() methods change the case of the characters.

let text = "JavaScript is Fun!";
console.log(text.toUpperCase()); // Output: JAVASCRIPT IS FUN!
console.log(text.toLowerCase()); // Output: javascript is fun!

2.4 Substring:
The substring(startIndex, endIndex) method extracts a portion of a string.

let sentence = "Programming is fascinating!";
let fragment = sentence.substring(12, 22);
console.log(fragment); // Output: is fascina

2.5 indexOf and lastIndexOf:

indexOf() returns the index of the first occurrence of a substring, and lastIndexOf() returns the index of the last occurrence.

let phrase = "JavaScript is powerful. JavaScript is versatile.";
console.log(phrase.indexOf("JavaScript")); // Output: 0
console.log(phrase.lastIndexOf("JavaScript")); // Output: 27

2.6 Replace:
The replace(oldString, newString) method replaces a specified substring with another.

let sentence = "I love programming in JavaScript.";
let updatedSentence = sentence.replace("JavaScript", "Python");
console.log(updatedSentence); // Output: I love programming in Python.

2.7 Trim:

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

let spacedText = "    Trim this text.    ";
let trimmedText = spacedText.trim();
console.log(trimmedText); // Output: Trim this text.

2.8 Split:

The split(separator) method splits a string into an array of substrings based on the specified separator.

let colors = "red,blue,green,yellow";
let colorArray = colors.split(",");
console.log(colorArray); // Output: ["red", "blue", "green", "yellow"]


Congratulations! You’ve covered the basics of JavaScript strings and some essential string methods. Practice these concepts by experimenting with your code and creating your examples. As you gain hands-on experience, you’ll become more confident in manipulating strings in JavaScript.

Remember, mastering these fundamentals is a crucial step toward becoming a proficient JavaScript developer.

Leave a Reply