Skip to main content

JavaScript String Methods Explained


JavaScript string methods are built-in functions that allow you to manipulate and work with strings in various ways. 

These methods make it easier to perform operations like finding, replacing, splitting, or modifying strings.

Common JavaScript String Methods

  1. length

    • Returns the number of characters in a string.

    let text = "JavaScript"; console.log(text.length); // Outputs: 10
  2. toUpperCase()

    • Converts all characters in a string to uppercase.

    let text = "JavaScript"; console.log(text.toUpperCase()); // Outputs: "JAVASCRIPT"
  3. toLowerCase()

    • Converts all characters in a string to lowercase.

    let text = "JavaScript"; console.log(text.toLowerCase()); // Outputs: "javascript"
  4. charAt(index)

    • Returns the character at a specific index in a string.

    let text = "JavaScript"; console.log(text.charAt(0)); // Outputs: "J"
  5. indexOf(substring)

    • Returns the index of the first occurrence of a specified substring, or -1 if the substring is not found.

    let text = "JavaScript"; console.log(text.indexOf("Script")); // Outputs: 4
  6. lastIndexOf(substring)

    • Returns the index of the last occurrence of a specified substring, or -1 if not found.

    let text = "JavaScript is great, and I love JavaScript!"; console.log(text.lastIndexOf("JavaScript")); // Outputs: 34
  7. includes(substring)

    • Returns true if a string contains a specified substring, otherwise false.

    let text = "JavaScript is fun"; console.log(text.includes("fun")); // Outputs: true
  8. startsWith(substring)

    • Returns true if a string starts with a specified substring, otherwise false.

    let text = "JavaScript"; console.log(text.startsWith("Java")); // Outputs: true
  9. endsWith(substring)

    • Returns true if a string ends with a specified substring, otherwise false.

    let text = "JavaScript"; console.log(text.endsWith("Script")); // Outputs: true
  10. substring(start, end)

    • Extracts characters from a string between two specified indices.

    let text = "JavaScript"; let part = text.substring(0, 4); // "Java"
  11. slice(start, end)

    • Similar to substring, but can also accept negative indices to count from the end.

    let text = "JavaScript"; let part = text.slice(4); // "Script"
  12. replace(searchValue, newValue)

    • Replaces the first occurrence of a specified substring with a new value.

    let text = "JavaScript is great"; let newText = text.replace("great", "awesome"); console.log(newText); // Outputs: "JavaScript is awesome"
  13. replaceAll(searchValue, newValue)

    • Replaces all occurrences of a specified substring with a new value.

    let text = "JavaScript is great. JavaScript is popular."; let newText = text.replaceAll("JavaScript", "JS"); console.log(newText); // Outputs: "JS is great. JS is popular."
  14. split(separator)

    • Splits a string into an array of substrings based on a specified separator.

    let text = "JavaScript,HTML,CSS"; let parts = text.split(","); console.log(parts); // Outputs: ["JavaScript", "HTML", "CSS"]
  15. trim()

    • Removes whitespace from both ends of a string.

    let text = " JavaScript "; console.log(text.trim()); // Outputs: "JavaScript"
  16. trimStart() (or trimLeft())

    • Removes whitespace from the beginning of a string.

    let text = " JavaScript"; console.log(text.trimStart()); // Outputs: "JavaScript"
  17. trimEnd() (or trimRight())

    • Removes whitespace from the end of a string.

    let text = "JavaScript "; console.log(text.trimEnd()); // Outputs: "JavaScript"
  18. concat(string1, string2, ...)

    • Joins two or more strings and returns a new concatenated string.

    let text1 = "Hello"; let text2 = "World"; let result = text1.concat(", ", text2); console.log(result); // Outputs: "Hello, World"
  19. repeat(count)

    • Returns a new string with a specified number of copies of the original string.

    let text = "JavaScript"; console.log(text.repeat(3)); // Outputs: "JavaScriptJavaScriptJavaScript"
  20. padStart(targetLength, padString)

    • Pads the current string with another string until it reaches the target length, starting from the beginning.

    let text = "5"; console.log(text.padStart(3, "0")); // Outputs: "005"
  21. padEnd(targetLength, padString)

    • Pads the current string with another string until it reaches the target length, starting from the end.

    let text = "5"; console.log(text.padEnd(3, "0")); // Outputs: "500"
  22. match(regex)

    • Searches a string for a match against a regular expression and returns the matches.

    let text = "The rain in Spain"; let result = text.match(/ain/g); console.log(result); // Outputs: ["ain", "ain"]
  23. search(regex)

    • Searches a string for a match against a regular expression and returns the index of the match, or -1 if not found.

    let text = "JavaScript"; console.log(text.search(/Script/)); // Outputs: 4
  24. toString()

    • Returns the string representation of a value.

    let number = 123; console.log(number.toString()); // Outputs: "123"
  25. valueOf()

    • Returns the primitive value of a string object.

    let text = new String("Hello"); console.log(text.valueOf()); // Outputs: "Hello"

These examples cover a wide range of string manipulation techniques available in JavaScript, providing powerful tools for working with text in your programs.

Popular posts from this blog

How to Check if Someone is Connected to Your Machine in Linux

In today's tech-savvy world, securing your machine is more crucial than ever. Imagine finding out that someone else is accessing your files or using your resources without permission. It’s unnerving, right? If you’re a Linux user, knowing how to check for unauthorized connections can help you safeguard your system. Here’s a straightforward guide on how to spot if someone is connected to your Linux machine. Understanding Network Connections Before jumping into the steps, let's get a grasp of what network connections mean. Every device connected to the internet has an IP address. When another user connects to your machine, they do it through this address. This connection could happen through various means, such as a direct network connection or even over the internet. Recognizing established connections is essential. Think of it like keeping an eye on who enters your home. You want to know who’s coming and going at all times, right? Using the netstat Command One of the most...

How to Set Up a Linux Web Server and Host an HTML Page Easily

To set up a web server in Linux, you must be comfortable working with the terminal. Linux relies heavily on command-line tools, meaning you’ll often type out instructions rather than relying on a graphical interface. If you’re new to Linux, it might feel intimidating at first, but learning a few essential commands can go a long way. Some commands you’ll frequently use include: cd : Change directories. ls : List the files in a directory. mkdir : Create a new folder. nano or vim : Open text editors directly in the terminal. sudo : Run commands with administrative privileges. Familiarity with these and other basic commands will ensure you can easily navigate directories, edit configuration files, and install the necessary software for your web server. Don’t worry, you don’t need to be a Linux expert—just confident enough to follow clear instructions. Linux Distribution and Access First, you’ll need a Linux operating system (also called a “distribution”) to work on. Popular opt...

SQL Server JDBC Driver: A Complete Guide

In this post, you'll find practical examples to get started with SQL Server and Java. From setting up the driver to executing SQL queries, we'll guide you every step of the way.  By the end, you'll know how to make your Java application communicate with SQL Server like a pro. Ready to enhance your database skills? Let's dive in. What is JDBC? Have you ever thought about how software connects to databases? JDBC is your answer. Java Database Connectivity, or JDBC, serves as the handshake between your Java application and databases like SQL Server. It's all about making data talk fluent Java. Overview of JDBC Architecture Think of JDBC as a structural framework with key components holding up a bridge of data exchange. Here's what makes up the JDBC architecture: Driver Manager : This is like the traffic cop directing different database drivers. It ensures the right driver talks to the right database. In simpler terms, it manages the connections and keeps ever...