1. Introduction
In JavaScript, strings are one of the most frequently used data types. A string represents a sequence of characters enclosed in single quotes (' '), double quotes (" "), or backticks ). JavaScript treats strings as immutable, meaning once created, their content cannot be changed.
To work effectively with textual data—such as names, messages, user input, or API responses—JavaScript provides a wide set of built-in string methods.
This article explains the most important string methods with examples and output, suitable for academic assignments and practical learning.
2. Creating Strings
Strings can be created in multiple ways:
let name1 = "Abhishek"; // Double quoteslet name2 = 'JavaScript'; // Single quoteslet name3 = Template String; // Template literal3. String Length
length property
The length property returns the number of characters in a string.
let text = "Hello World";console.log(text.length); // 114. String Search Methods
4.1 indexOf()
Returns the index of the first occurrence of a substring.
"JavaScript".indexOf("Script"); // 44.2 lastIndexOf()
Returns the index of the last occurrence of a substring.
"banana".lastIndexOf("a"); // 54.3 includes()
Checks whether a substring exists.
"Hello".includes("ell"); // true4.4 startsWith() and endsWith()
"JavaScript".startsWith("Java"); // true
"JavaScript".endsWith("Script"); // true5. String Extraction Methods
5.1 slice(start, end)
Extracts part of a string.
"JavaScript".slice(0, 4); // "Java"
5.2 substring(start, end)
Similar to slice, but does not support negative indexes.
"Programming".substring(0, 3); // "Pro"
5.3 substr(start, length) (Deprecated)
Extracts part of a string based on a given length.
"Learning".substr(0, 4); // "Lear"
6. Case Conversion Methods
6.1 toUpperCase()
"hello".toUpperCase(); // "HELLO"
6.2 toLowerCase()
"HELLO".toLowerCase(); // "hello"
7. Replace Methods
7.1 replace()
Replaces only the first match.
"Hello World".replace("World", "JavaScript");
// "Hello JavaScript"
7.2 replaceAll()
Replaces all occurrences.
"banana".replaceAll("a", "o");
// "bonono"
8. Trim Methods
8.1 trim()
Removes spaces from both sides.
" hello ".trim(); // "hello"
8.2 trimStart() / trimEnd()
" hello".trimStart(); // "hello"
"hello ".trimEnd(); // "hello"
9. String Joining and Splitting
9.1 concat()
Joins multiple strings together.
"Hello".concat(" ", "World"); // "Hello World"
9.2 split()
Splits a string into an array.
"red,green,blue".split(",");
// ["red", "green", "blue"]
10. Character Methods
10.1 charAt(index)
"JavaScript".charAt(0); // "J"
10.2 charCodeAt(index)
Returns the Unicode value.
"A".charCodeAt(0); // 65

Join the conversation! Your thoughts help the community grow.