JavaScript  

String Methods in JavaScript – A Complete Article

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 quotes
let name2 = 'JavaScript';      // Single quotes
let name3 = Template String; // Template literal

3. String Length

length property

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

let text = "Hello World";
console.log(text.length);  // 11

4. String Search Methods

4.1 indexOf()

Returns the index of the first occurrence of a substring.

"JavaScript".indexOf("Script");  // 4

4.2 lastIndexOf()

Returns the index of the last occurrence of a substring.

"banana".lastIndexOf("a");  // 5

4.3 includes()

Checks whether a substring exists.

"Hello".includes("ell");  // true

4.4 startsWith() and endsWith()

"JavaScript".startsWith("Java");   // true
"JavaScript".endsWith("Script");   // true

5. 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

11. Template Literals

Template literals allow multi-line strings and variable insertion.

let name = "Abhishek";
let msg = `Hello, ${name}! Welcome to JavaScript.`;

12. Modern Useful Methods

12.1 repeat()

Repeats a string.

"ha".repeat(3);  // "hahaha"

12.2 padStart() and padEnd()

Adds padding to the beginning or end.

"5".padStart(3, "0");  // "005"
"5".padEnd(3, "0");    // "500"

13. Summary Table of Common Methods

MethodDescriptionExample
lengthString length"abc".length → 3
indexOf()Search substring"hi".indexOf("i")
includes()Check existence"hello".includes("he")
slice()Extract part"hello".slice(1,4)
toUpperCase()Convert to uppercase"hi".toUpperCase()
replace()Replace first occurrence"hi hi".replace("hi","bye")
split()Convert to array"a,b".split(",")
trim()Remove spaces" hi ".trim()
repeat()Repeat string"ha".repeat(2)

14. Best Practices

  1. Prefer slice() over substr() because substr() is outdated.

  2. Use template literals for clean formatting.

  3. Avoid using the + operator repeatedly to build long strings.

  4. Use includes() instead of indexOf() when checking true/false conditions.

  5. For removing extra spaces, use trim() before saving user input.

15. Conclusion

JavaScript offers powerful and easy-to-use string methods that help developers handle text efficiently. These methods are essential for processing user input, formatting output, working with APIs, and building real applications. Mastering string methods is an important step in becoming confident with JavaScript programming.