Skip to content
the poor coder

JavaScript String Methods

Poor Man
Published
Reading time
3 min read
On this page

JavaScript String Methods#

As someone who has worked with JavaScript for a while, I have found that one of the most important things to know are the various String methods available. These methods allow you to manipulate and modify strings in various ways.

Common String Methods#

  • charAt(): returns the character at a specified index in a string
  • concat(): combines two or more strings into one string
  • includes(): checks whether a string contains a specified substring
  • indexOf(): returns the index of the first occurrence of a specified value in a string
  • lastIndexOf(): returns the index of the last occurrence of a specified value in a string
  • replace(): replaces a specified value with another value in a string
  • substring(): returns the part of a string between two indexes
  • toLowerCase(): returns the string in lowercase letters
  • toUpperCase(): returns the string in uppercase letters
  • trim(): removes whitespace from both sides of a string

Let's take a look at an example of using some of these methods:

Text

let phrase = "The quick brown fox jumps over the lazy dog";

console.log(phrase.charAt(0)); // Output: "T"
console.log(phrase.concat(" and the cat")); // Output: "The quick brown fox jumps over the lazy dog and the cat"
console.log(phrase.includes("fox")); // Output: true
console.log(phrase.indexOf("the")); // Output: 31
console.log(phrase.lastIndexOf("the")); // Output: 31
console.log(phrase.replace("fox", "kangaroo")); // Output: "The quick brown kangaroo jumps over the lazy dog"
console.log(phrase.substring(4, 9)); // Output: "quick"
console.log(phrase.toLowerCase()); // Output: "the quick brown fox jumps over the lazy dog"
console.log(phrase.toUpperCase()); // Output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
console.log("   hello   ".trim()); // Output: "hello"

Regular Expressions#

Another powerful tool for working with strings in JavaScript is regular expressions. Regular expressions are patterns that can be used to match and manipulate strings in various ways.

Here are some common methods for working with regular expressions:

  • test(): tests whether a string matches a regular expression
  • match(): searches a string for a regular expression and returns the matched text
  • replace(): replaces a specified value with another value in a string, using a regular expression

Here's an example of using regular expressions to replace all instances of the word "fox" in our previous example:

Text

let phrase = "The quick brown fox jumps over the lazy dog";

console.log(phrase.replace(/fox/g, "kangaroo")); // Output: "The quick brown kangaroo jumps over the lazy dog"

In this example, we use the /fox/g regular expression pattern to replace all instances of the word "fox". The g flag tells JavaScript to perform a global search, rather than just replacing the first instance.

Regular expressions can be complex, but they provide a lot of power and flexibility for working with strings. If you're interested in learning more, there are many resources available online to help you get started.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC