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

Picture this: you glance at your system monitor and notice your CPU is humming along even though you're not running anything demanding. Or maybe your internet feels sluggish for no obvious reason. A small, uneasy thought creeps in — is someone else on my machine right now? For Linux users, this isn't something you have to wonder about. Linux ships with a powerful set of built-in tools that let you see exactly who's connected, who's logged in, and what your network is doing at any given moment. You don't need to be a security expert to use them — you just need to know where to look. This guide walks you through the practical, no-nonsense steps to check for unauthorized connections on your Linux system, with real commands you can run right now. Why Monitoring Network Connections Matters Every device on a network — including your own Linux machine — communicates using an IP address. When another device or user connects to your system, that connection shows up as a trac...

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

Setting up a web server on Linux means spending a fair amount of time in the terminal — Linux leans heavily on the command line rather than clicking through menus, so you'll be typing out instructions more often than not.  If you're new to this, it can feel a little intimidating at first, but the good news is you don't need to become a Linux wizard overnight. A handful of core commands will get you surprisingly far. A few you'll lean on constantly: cd — move between directories ls — see what's in the current directory mkdir — create a new folder nano or vim — edit files right there in the terminal sudo — run something with administrator privileges Get comfortable with these and you'll be able to navigate around, tweak configuration files, and install software without much trouble. You don't need to memorize everything — you just need to be confident enough to follow along with clear instructions, which is exactly what this guide aims to give you....

C++ vcpkg Manifest Mode + CMake

 If you've ever tried to install a C++ library and felt like you were assembling furniture without instructions, this article is for you. We're going to talk about vcpkg manifest mode and how it works with CMake , and I'm going to explain it like you're five years old (in a good way — no judgment here). First, Let's Talk About the Problem In most programming languages, adding a library is easy. Python has pip install requests . JavaScript has npm install express . You type one command, and boom, the library shows up in your project. C++ never really had that. For decades, if you wanted to use a library like fmt or nlohmann/json , you had to: Download the source code yourself Figure out how to compile it Tell your compiler where to find the headers Tell your linker where to find the compiled binaries Cry a little vcpkg is Microsoft's answer to this mess. It's a package manager for C++ — like pip or npm , but for C++ libraries. And manifest mode...