Skip to main content

Command Palette

Search for a command to run...

JavaScript String Methods You Must Know

Updated
3 min read
JavaScript String Methods You Must Know

“Text is everywhere — your job is to control it”

From user input to API responses, almost everything in JavaScript involves strings.

Names, emails, messages, URLs — all strings.

But raw strings are rarely useful. You almost always need to process, clean, extract, or transform them.

That’s where string methods come in.


What is a String in JavaScript?

A string is a sequence of characters enclosed in quotes.

let name = "Mohit";
let message = 'Hello World';

Strings are primitive values,


Important Concept: Strings Are Immutable

let str = "hello";
str.toUpperCase();

console.log(str); // still "hello"

Methods do NOT modify the original string. They return a new string.

let updated = str.toUpperCase();

In This Blog, We’ll Cover

Accessing & Basics

  • length

  • indexing

Extracting Parts of a String

  • slice()

  • substring()

Transforming Strings

  • toUpperCase()

  • toLowerCase()

  • trim()

Searching in Strings

  • includes()

  • indexOf()

Modifying & Converting

  • replace()

  • split()


Accessing & Basics

length → Get String Size

let str = "JavaScript";

console.log(str.length); // 10

Indexing → Access Characters

let str = "JavaScript";

console.log(str[0]); // J
console.log(str[4]); // S

Extracting Parts of a String

slice()

let str = "JavaScript";

console.log(str.slice(0, 4)); // Java
console.log(str.slice(4));    // Script

substring()

let str = "JavaScript";

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

Transforming Strings

toUpperCase() / toLowerCase()

let str = "hello";

console.log(str.toUpperCase()); // HELLO
console.log(str.toLowerCase()); // hello

trim()

let str = "   hello world   ";

console.log(str.trim()); // "hello world"

Searching in Strings

includes()

let str = "JavaScript";

console.log(str.includes("Script")); // true

indexOf()

let str = "JavaScript";

console.log(str.indexOf("S")); // 4

Modifying & Converting

replace()

let str = "Hello World";

console.log(str.replace("World", "JavaScript"));
// Hello JavaScript

split()

let str = "apple,banana,orange";

let result = str.split(",");
console.log(result);
// ["apple", "banana", "orange"]

Real Example

let email = "   USER@GMAIL.COM   ";

let cleanEmail = email.trim().toLowerCase();

console.log(cleanEmail); // user@gmail.com

Quick Comparison: slice vs substring

Method Negative Index Use Case
slice() ✅ Yes More flexible
substring() ❌ No Basic usage

Final Thought

String methods are not just utilities — they are how you transform raw text into usable data in real applications.