String Methods: A Complete Guide

JavaScript uses strings for everything from processing user input to displaying messages. Additionally, JavaScript provides us with robust built-in string methods that make text manipulation incredibly simple and effective.

This article will explore the most important and frequently used string methods that developers need to be familiar with.

๐Ÿ“Œ1. Length Find out how many characters there are in a string.

const message = "Hello, world!";
console.log(message.length); // 13

๐Ÿ“Œ2.toUpperCase() and toLowerCase() Convert a string to upper or lower case.

const name = "John Doe";
console.log(name.toUpperCase()); // "JOHN DOE"
console.log(name.toLowerCase()); // "john doe"

๐Ÿ“Œ3.includes() Check if a string contains a substring.

const phrase = "Learning JavaScript is fun!";
console.log(phrase.includes("JavaScript")); // true
console.log(phrase.includes("Python"));     // false

๐Ÿ“Œ4. indexOf() and lastIndexOf() Get the position of the substring within a string.

const sentence = "The quick brown fox jumps over the lazy dog.";
console.log(sentence.indexOf("fox"));      // 16
console.log(sentence.lastIndexOf("the"));  // 31

๐Ÿ“Œ5.slice() Extract a portion of a string.

const text = "JavaScript";
console.log(text.slice(0, 4));  // "Java"
console.log(text.slice(4));     // "Script"

๐Ÿ“Œ6.substring() Similar to slice() except it doesn't accept negative indexes

console.log(text.substring(0, 4)); // "Java"

๐Ÿ“Œ7.replace() and replaceAll() Replaces part of a string.

const greeting = "Hello there!";
console.log(greeting.replace("Hello", "Hi")); // "Hi there!"

const messy = "apple, banana, apple";
console.log(messy.replaceAll("apple", "orange")); // "orange, banana, orange"

๐Ÿ“Œ8.trim() Trim off unwanted padding(white spaces).

const userInput = "    Hello World!    ";
console.log(userInput.trim()); // "Hello World!"

๐Ÿ“Œ9.split() Convert a string into an array.

const csv = "red,green,blue";
console.log(csv.split(",")); // ["red", "green", "blue"]

๐Ÿ“Œ10.charAt() and charCodeAt() Access a character or access its ASCII code.

const word = "JavaScript";
console.log(word.charAt(0));     // "J"
console.log(word.charCodeAt(0)); // 74

โšก Bonus Recommendations

  • Use template literals (`) for easier string building.

  • Don't forget to combine methods like trim() and toLowerCase() for consistent, clean input handling.

๐ŸŽฏ In Closing

Knowing these string methods will be worth hours of code and create code that is more efficient and cleaner for all of your JavaScript projects.

"Great things are done by a series of small things brought together.
In coding, it's the small things โ€” like knowing the right method โ€” that make the biggest difference."

Every little trick you learn stacks up to make you a powerful developer. Keep building, keep mastering! ๐Ÿš€

If you liked it, then follow me to get more helpful tips!

Did I miss a good string method? Let me know in the comments! ๐Ÿ‘‡

1
Subscribe to my newsletter

Read articles from Harshvardhan khamkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshvardhan khamkar
Harshvardhan khamkar