JavaScript String Methods Cheatsheet

Javascript Illustration

JavaScript provides a wide range of string methods that can be used to manipulate and modify strings. Here are 15 commonly used string methods in JavaScript, along with examples and their outputs.

1. length

  • Description: Returns the length of a string.
  • Example: str.length;
  • Output: 21

2. trim()

  • Description: Removes whitespace from both ends of a string.
  • Example: str.trim();
  • Output: "Hello, JavaScript!"

3. toUpperCase()

  • Description: Converts a string to uppercase.
  • Example: str.toUpperCase();
  • Output: "HELLO, JAVASCRIPT!"

4. toLowerCase()

  • Description: Converts a string to lowercase.
  • Example: str.toLowerCase();
  • Output: "hello, javascript!"

5. charAt()

  • Description: Returns the character at a specific index in a string.
  • Example: str.charAt(7);
  • Output: "J"

6. indexOf()

  • Description: Finds the first occurrence of a substring in a string.
  • Example: str.indexOf("JavaScript");
  • Output: 8

7. includes()

  • Description: Checks if a substring is present in a string.
  • Example: str.includes("Hello");
  • Output: true

8. substring()

  • Description: Extracts characters between two indices in a string.
  • Example: str.substring(0, 7);
  • Output: "Hello"

9. slice()

  • Description: Similar to substring, but allows negative indices.
  • Example: str.slice(2, -2);
  • Output: "Hello, JavaScript"

10. replace()

  • Description: Replaces a substring with another string.
  • Example: str.replace("JavaScript", "World");
  • Output: "Hello, World!"

11. split()

  • Description: Splits a string into an array by a separator.
  • Example: str.split(" ");
  • Output: ["", "", "Hello,", "JavaScript!", "", ""]

12. concat()

  • Description: Concatenates two or more strings.
  • Example: str.concat("Let's code!");
  • Output: "Hello, JavaScript!Let's code!"

13. startsWith()

  • Description: Checks if a string starts with a specified substring.
  • Example: str.startsWith("Hello");
  • Output: false (due to leading spaces)

14. endsWith()

  • Description: Checks if a string ends with a specified substring.
  • Example: str.endsWith("!");
  • Output: true

15. repeat()

  • Description: Repeats a string a specified number of times.
  • Example: str.repeat(2);
  • Output: "Hello, JavaScript!Hello, JavaScript!"

These string methods can be used to perform various operations on strings in JavaScript, making it easier to manipulate and modify text data in your applications.

More News

Google has unveiled a transformative update for its Google Play Games for PC platform, aiming to bridge the gap between Android and

The Lenovo ThinkPad X1 2-in-1 Gen 10 Aura Edition represents a significant leap forward in the realm of convertible laptops, blending cutting-edge

In a move that has tech enthusiasts buzzing, Honor has revealed key specifications for its highly anticipated 300 series smartphones. The announcement,

The upcoming OnePlus 13 is set to make its debut before the end of October, with leaked information on Weibo revealing its

Advertisment

More Articles

Australia’s casino hotels offer a unique blend of luxury, entertainment, and excitement, making them some of the most sought-after destinations for both

Croatia’s coastline is a treasure trove of hidden beaches, offering pristine waters and secluded spots away from the tourist crowds. From rocky

Lenovo’s Tech World 2024 event, held in Seattle on October 15, 2024, showcased the company’s most ambitious AI innovations to date, reinforcing

In the ever-evolving world of gaming laptops, finding a powerful machine that doesn’t break the bank can be a challenge. However, the

Advertisment