DEV Community

Victor Olaoye
Victor Olaoye

Posted on • Originally published at Medium on

String Methods In JavaScript

A blurry block of JavaScript code on a laptop.
Photo by Shahadat Rahman on Unsplash

In this article, I’ll be covering different string methods that helps us work with strings in JavaScript. Strings are considered primitives in JavaScript because it’s a single value; JavaScript treats primitives as if they were objects and provides methods to call them.

charAt()

The charAt() method is used to return the character at a specified position in a string.

From the image, I declared a variable called str and assigned “hello world” to it. Using the charAt() method, I did console.log(str.charAt(0)) and it prints ‘h’ to the console

From the code block in the image, we can see that it prints the first character to the console(“h”). If we want to print the 5th character of a given string to the console, then we replace 0 with 4.

concat()

The concat() method was coined from the word concatenate, which means joining two or more strings together. See how it works from this example;

Three different variables were declared with three different strings assigned to them respectively and then the concat() method was used to join the three strings together

From the example above, two strings were joined together using the concat() method

indexOf()

The indexOf() method returns the position of the first occurrence of a value in a string. Let’s see how it works;

A declared variable assigned to a string — “hello join, nice to meet you”. The indexOf() method is used to get the position of “nice” — str.indexOf(“nice”) which prints 12 to the console.

lastIndexOf()

Unlike indexOf(), the lastIndexOf() method returns the position of the last occurrence of a value in a string. See the example below;

A declared variable assigned to a string — “hello join, nice to meet you”. The indexOf() method is used to get the position of “you” — str.lastIndexOf(“you”) which prints 25 to the console.

Caveat: Both the indexOf() and lastIndexOf() methods are case sensitive.

replace()

This is a generic method for replacing and searching. It can also be used with regular expressions.

A variable assigned to a string — “hello world”, then the replace method is used to replace “world” with “john” — str.replace(“world”, “john”). It prints “hello john” to the console.

search()

The search() method returns the position of the first match or -1 if not found. Let’s see how it works in the example below;

A variable assigned a string — “hello world”, then the search() method is used to search for the position of “world“ from the string — str.search(“world”). It prints 6 to the console.

slice()

The slice()method is just one of the three ways to get a substring in JavaScript and it’s syntax is slice(start [, end]). It returns the extracted part in a new string. For instance;

A variable assigned to a string — “hello”, then the slice() method is used to return the letters from index 0 to index 4 — str.slice(0, 4). It prints “hell” to the console.

substr()

The substring(start [, end]) is almost the same as slice — it’s also a way to get substring in JavaScript except that it allows the start to be greater than the end and also allows negative starts. For instance;

A variable assigned a string — “JavaScript”, then the substr() method is used to return the letters from index 4 to index 6— str.substr(4, 6). It prints “Script” to the console.

substring()

The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. Let’s see how it works;

A variable assigned a string — “hello world”, then the substring() method is used to return the characters in-between index 5 and index 8- str.substring(5, 8). It prints “wo” to the console.

toUpperCase()

It changes the case of all characters in the string to uppercase.

A variable assigned a string — “hello john” all in lowercase, then the toUpperCase() method is used to change every letter in the string to uppercase- str.toUpperCase(). It prints “HELLO JOHN” to the console.

toLowerCase()

It’s the direct opposite of the toUpperCase() method. It changes the case of all characters in the string to lowercase.

A variable assigned a string — “HELLO JOHN” all in uppercase, then the toLowerCase() method is used to change every letter in the string to lowercase- str.toLowerCase(). It prints “hello john” to the console.

valueOf()

The valueOf() method is the default method for string, used internally by JavaScript. This method returns the primitive value of a string. The valueOf() method can be used to convert a string object into a string.

A variable assigned a string — “Hello World!”, then the valueOf() method is used to get the primitive value of the string - str.valueOf(). It prints “Hello World!” to the console.

trim()

This is a very helpful method that removes spaces from the beginning and end of a string. For instance;

A variable assigned a string — “ hello world ” with four tab spaces at the beginning and end of the string, then the trim() method is used to remove the spaces at the beginning and end of the string — str.trim(). It prints “hello world” to the console.

toString()

This method returns a string as a string. It can also be used to convert a string object to string. For instance;

split()

The split() method splits a string into an array of substrings and it returns a new array.

Used the .split() method on a string — console.log(‘12–34–56’.split(‘-’)). It prints an array [‘12’, ‘34’, ‘56’].

charCodeAt()

We can use the charCodeAt() to get the Unicode of any character in a string.

includes()

This method returns true if a string contains a specified string, otherwise it returns false. The includes() method is case sensitive.

A variable assigned a string — “Hello World, welcome to the universe!”, then the includes() method is used to determine if the string contains “welcome” — str.includes(“welcome”). It prints true to the console.

match()

The match() method finds matches for regular expressions in a string. It has three modes:

  1. If the regexp doesn’t have flag g, then it returns the first match as an array with capturing groups and properties index , input (input string, equals str)
  2. If the regexp has flag g, then it returns an array of all matches as strings, without capturing groups and other details.
  3. If there are no matches, no matter if there’s flag g or not, null is returned.

Let’s see an example of how this method works below:

A variable assigned a string — “JavaScript TypeScript NativeScript”, then the match() method is used to check if any word from the string matches “Script” — str.match(/Script/g”). It then prints an array of all matches — [‘Script’, ‘Script’, ‘Script’].

Summary

In this article, we’ve seen different string methods, their descriptions and how to apply them in JavaScript.

References

. This whole article was inspired by codingtute JavaScript string methods cheat sheet I saw a while ago

. You can also take a look at JavaScript.info to learn more

. Also, w3schools is also a great resource to learn about strings in-depth.

Top comments (0)