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

The Commodore 64, the best-selling single computer model of all time, is experiencing an unprecedented renaissance. Decades after the original Commodore International

In what is being called a “watershed moment” for digital regulation, Meta has announced the removal of nearly 550,000 accounts across its

What insiders just found Windows 11 preview builds show a hidden button in File Explorer’s command bar that only appears when you

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

Advertisment

More Articles

What IPTV actually is IPTV (Internet Protocol Television) delivers live TV and on-demand video over IP networks instead of classic broadcast (DVB/terrestrial/cable

In an era where smartphone manufacturers race to produce the most “perfect,” AI-sharpened images, a growing community of photographers is heading in

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

Advertisment