codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

22 / 65 topics
21JavaScript Strings22JavaScript String Methods23JavaScript Numbers24JavaScript Math Object25JavaScript Arrays26JavaScript Array Methods27JavaScript Array Iteration
Tutorials/JavaScript/JavaScript String Methods
🌐JavaScript

JavaScript String Methods

Updated 2026-05-12
20 min read

JavaScript String Methods

In the previous topic, we explored what strings are in JavaScript and how to create them. Now, let's dive into the various methods available to manipulate strings effectively. Understanding these methods is crucial for any developer working with text data.

Introduction

Strings are a fundamental data type in JavaScript, and they come with a plethora of built-in methods that allow you to perform operations like searching, replacing, splitting, and transforming text. These methods can greatly simplify your code when dealing with string manipulations. In this tutorial, we'll cover some of the most commonly used string methods: length, slice, substring, replace, toUpperCase, toLowerCase, concat, trim, and split.

Core Content

1. Length

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

script.js
1let str = "Hello, world!";
2console.log(str.length); // Output: 13
Output
13

2. Slice

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

script.js
1let str = "Hello, world!";
2console.log(str.slice(7, 12)); // Output: "world"
Output
world

3. Substring

The substring() method is similar to slice(), but it doesn't accept negative indices.

script.js
1let str = "Hello, world!";
2console.log(str.substring(7, 12)); // Output: "world"
Output
world

4. Replace

The replace() method replaces a specified value with another value in a string.

script.js
1let str = "Hello, world!";
2console.log(str.replace("world", "universe")); // Output: "Hello, universe!"
Output
Hello, universe!

5. toUpperCase

The toUpperCase() method converts a string to uppercase letters.

script.js
1let str = "Hello, world!";
2console.log(str.toUpperCase()); // Output: "HELLO, WORLD!"
Output
HELLO, WORLD!

6. toLowerCase

The toLowerCase() method converts a string to lowercase letters.

script.js
1let str = "Hello, world!";
2console.log(str.toLowerCase()); // Output: "hello, world!"
Output
hello, world!

7. Concat

The concat() method combines two or more strings.

script.js
1let str1 = "Hello, ";
2let str2 = "world!";
3console.log(str1.concat(str2)); // Output: "Hello, world!"
Output
Hello, world!

8. Trim

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

script.js
1let str = " Hello, world! ";
2console.log(str.trim()); // Output: "Hello, world!"
Output
Hello, world!

9. Split

The split() method splits a string into an array of substrings.

script.js
1let str = "Hello, world!";
2console.log(str.split(", ")); // Output: ["Hello", "world!"]
Output
["Hello", "world!"]

Practical Example

Let's create a simple program that takes a user input string, converts it to uppercase, and splits it into words.

script.js
1let userInput = prompt("Enter a sentence:");
2let upperCaseSentence = userInput.toUpperCase();
3let wordsArray = upperCaseSentence.split(" ");
4console.log(wordsArray);
Terminal
$ node script.js
Enter a sentence: Hello, world!
["HELLO,", "WORLD!"]

Summary

MethodDescription
lengthReturns the number of characters in a string.
slice()Extracts a section of a string and returns it as a new string.
substring()Similar to slice(), but doesn't accept negative indices.
replace()Replaces a specified value with another value in a string.
toUpperCase()Converts a string to uppercase letters.
toLowerCase()Converts a string to lowercase letters.
concat()Combines two or more strings.
trim()Removes whitespace from both ends of a string.
split()Splits a string into an array of substrings.

What's Next?

Now that you've learned about JavaScript string methods, it's time to explore another fundamental data type: numbers. In the next topic, we'll dive into how to work with numerical values in JavaScript, including arithmetic operations, number methods, and more.

Stay tuned for "JavaScript Numbers"!


PreviousJavaScript StringsNext JavaScript Numbers

Recommended Gear

JavaScript StringsJavaScript Numbers