JavaScript String: Everything You Need to Know about the String Type in JavaScript

JavaScript String: Everything You Need to Know about the String Type in JavaScript


The String Data Structure in JavaScript

Strings are one of JavaScript’s few native data types and are a simple way to store and manipulate letters and other characters.

There are three common ways of creating strings in JavaScript:

  • Single quotes ()
  • Double quotes ()
  • Template Literals(```)

Strings are quite common in JavaScript, so knowing how to use and manipulate them is important.

Here is a string created with single quotes:

Here is a string created with double quotes:

Here is a string created with template literals:

Creating strings is important, but once a string exists there are many operations you can perform on them. Let’s investigate.

JavaScript String Operations

Many operations can be performed on JavaScript strings to manipulate and analyze them. Let’s take a look at some of the most common operations.

JavaScript String Length

One of the most frequently used built-in operations of JavaScript strings is the ‘.length’ property, which is used to return the length of any string.

Here’s an example:

Concatenating JavaScript Strings

Sometimes you’ll have more than one string and you’ll want to combine them into a single string. This is called concatenation and can be done in a few different ways:

  • Using the + operator
  • Using the .concat() method on a spring

Here’s an example of how you can use the + operator:

Here are some examples of how you can use the .concat() method to concatenate two or more strings:

And here is how you would concatenate more than two strings together:

Accessing a Character in a String

Sometimes you have a string and want to access an individual character in the string. With strings in JavaScript, you can do this the same way that you might access an individual element in an array, by using the character’s index.

Comparing Strings

There will likely be times when you have strings from different sources and you’ll want to see if they’re the same. When you end up in this situation you want to compare two strings for equality, and JavaScript has a simple syntax for doing so:

Converting Other Data Types to Strings

Sometimes you have data in another data type that you want to convert into a string. Javascript has a few ways of doing this.

One of the most common ways is to use the String() function to turn data into a string. Here’s how it works for a number:

And here’s how you can use the String() function to convert other data types into a string:

But this isn’t the only way you can convert data types into a string. There’s a special way that you can convert JavaScript Objects into strings that are formatted particularly well for transmission in requests and responses, known as JSON.

You can use the JSON.strinfiy() function to convert JavaScript objects into this format like so:

Escaping Characters in JavaScript Strings

There are some combinations of characters that have special meaning in strings, such as

  • \n: this is how line breaks are represented in strings
  • \t: this is how tabs are represented in strings

And many more. But sometimes you don’t want another program to pay special attention to these combinations of characters, and you can do this by “escaping” those characters.

You can escape characters by putting the \ character in front of one of the characters that you want programs to ignore. It’s the equivalent of saying “don’t do anything special with this next character – just display it as you would normally.”

Changing Lowercase Strings to Uppercase Strings

Sometimes you’ll have lowercase strings but want to convert them into uppercase strings. JavaScript strings have a built-in method to make this easy: the .toUppercase() method.

Here’s how you can use it:

Changing Uppercase Strings to Lowercase Strings

The reverse is also true – sometimes you’ll have uppercase strings and want to convert them to lowercase strings. You can use the .toLowercase() method to return the lowercase version of a string:

Nullish Coalescing