JavaScript is a simple and powerful programming language used to create software and technology for many different applications. From web development to programming interfaces and software for NASA, it has grown in popularity since its inception. The language is so robust that there are many topics to learn and many ways to use them.

This post will cover JavaScript string methods, string methods modify and manipulate string values. You will learn about some built-in string methods and others that can perform various tasks on strings. Furthermore, you will see some examples of syntax you can use in CodePen to help solidify your understanding.

Without further delay, let's jump right in.

Download Now: An Introduction to Java & JavaScript

JavaScript String Explained

The JavaScript string is a data type used to convey information as text, which differs from numerical data types like int. Specifically, an int value can only be a number, but strings can contain any character. Because of this, they are used commonly in software development across industries and languages.

Because of the popularity and need for string data, JavaScript offers many different methods for interacting with and modifying strings. There are over thirty distinct methods for modifying and manipulating string values.

Let’s dive deeper into some of the most common methods available in JavaScript.

Built-in JavaScript String Methods

In many languages, strings are objects and thus have several properties and methods to build on their functionality. In JavaScript, however, strings are a primitive data type and thus do not have any built-in methods or properties. Despite this fact, JavaScript does support the use of many methods and a couple of properties on strings.

Because JavaScript treats strings as an object when methods or properties get executed on them, they still have that functionality. This ability is due to the String object in the JavaScript library, though it’s not intended for creating string objects. Creating string objects results in slower processing and potentially errant results, so use them cautiously.

Now that you've learned the theory behind methods and strings let's dive into how they work together.

JavaScript String Methods

The usefulness of strings is boundless, and it is crucial to understand how you can use them. Arguably the most common thing you will do with them is modifying them in some way or another. To facilitate that process, JavaScript includes a string object allowing methods and properties to be executed on string values.

Check out this video on string methods to learn more about what they are.

As an example, one use of strings is to sterilize any malicious code from being injected into a program or piece of software. This process happens using a regex — or regular expression — which in no way looks like regular expressions.

Regular expressions are patterns on strings to ensure they meet specific formatting requirements. Enough on the theory behind methods, now let’s dive into some code examples.

How to Use String Methods in JavaScript

Truthfully, using string methods is relatively easy to do. However, understanding what's happening is a different topic. Let’s clarify this with an example, using a string method is simply a matter of understanding the syntax. Let’s look at a code example of the syntax below.

let str = “Doctor Who”;

That is the bare bones of literally every string method, it starts with a variable holding a primitive string value. Then you chain the desired method to the string variable using the dot notation and finish it off with parenthesis and a semicolon. Here is a code snippet showing how to use the trim() method.

let str = “Yes, but Doctor Who?”;
/*Would return the string “Yes,butDoctorWho?”*/

Simple enough, right? Well, what if that method requires a parameter? In that case, the method will accept some criteria on what to do with the string value. This behavior changes things a little, let's look at a simple example of a method with parameters. The code below does the same as the code above, removing any whitespace from a string. Only this time it's done using the replace() method.

The replace method accepts two parameters, the first is what the string method should look for, and the second is the value to replace with the target.

let str = “I’m a Timelord from the planet Gallifrey.”;
/*Would return the string “I’maTimelordfromtheplanetGallifrey.”*/

The above code uses a regular expression that equates to “all-white space, inclusive”, which tells JavaScript to remove all occurrences of any white space.

String Methods in JavaScript With Examples

There are a lot of string methods that you can use with values for different applications. There are far too many to cover all of them in one post. Instead, let’s go over some of the more popular and commonly used ones. Below is a cheat sheet of some of the popular JavaScript string methods.

An image of some popular JavaScript string methods.

Assuming we use the following string value assigned to a variable as our target string, the methods would look like the following examples.

let str = “I’m a Timelord from the planet Gallifrey.”;


/*Would return the string, "i’m a timelord from the planet gallifrey."*/


let num = 904;
/*Would return “904”*/


/*Would return the string, "I’M A TIMELORD FROM THE PLANET GALLIFREY."*/


str.substring(31, 40);
/*Would return the string “Gallifrey”. NOTE: Excluding the second index will return all characters after the start index.*/


/*Would return boolean true*/


/*Would return an array object with the matched value. Regular expressions can also be used.*/


str.concat(” I’m The Doctor, look me up!”);
/*Would return “I’m a Timelord from the planet Gallifrey. I’m The Doctor, look me up!”*/


This list is by no means exhaustive, there are many more methods, and some are much less known than others. An example of these lesser-known methods would be the padStart() and padEnd() methods which allow you to add a string to the beginning and end of a string value.

Getting Started Using JavScript String Methods

There is a lot to learn under the umbrella of strings and string methods, the best way to solidify all the information is to practice using them. There are an endless number of applications that use strings, and even more ways methods can help.

Working with string methods, you can learn to understand the boundaries and behaviors and how to use methods together. By combining string methods, you can perform powerful tasks and do almost anything your software may require.



Originally published Jun 28, 2022 6:00:00 AM, updated June 28 2022