String Methods In JavaScript
Tags: javascript, datastructures, stringinjavascript
Published: 2022-10-08 11:38:03

---
title: String Methods In JavaScript
published: true
date: 2022-10-01 04:47:02 UTC
tags: javascript,js,datastructures,stringinjavascript
canonical_url: https://medium.com/@olaoyevick/string-methods-in-javascript-89df0b38fee8
---

![A blurry block of JavaScript code on a laptop.](https://cdn-images-1.medium.com/max/1024/0*fmot_9AivddSmi8Q)
_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](https://cdn-images-1.medium.com/max/517/1*r8HYps9HwnqbfHJ0c_tk8g.png)

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](https://cdn-images-1.medium.com/max/515/1*rEDznux0UD8iSPrBqZ3k3w.png)

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.](https://cdn-images-1.medium.com/max/515/1*qtFBqRXBHbcJYW2kmFM2Vg.png)

#### 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.](https://cdn-images-1.medium.com/max/532/1*oD0HhmEBGS9A_PPYdql9lQ.png)

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.](https://cdn-images-1.medium.com/max/530/1*AN7H6pzcNk-BAvYvDDo4xw.png)

#### 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.](https://cdn-images-1.medium.com/max/524/1*k3s8C2YZCflSqnCGeVIPxw.png)

#### 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.](https://cdn-images-1.medium.com/max/375/1*bINRUXCfcsg1Btvsbtjx3g.png)

#### 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.](https://cdn-images-1.medium.com/max/376/1*PTWC32mO1wDO9UbxazYkBQ.png)

#### 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.](https://cdn-images-1.medium.com/max/376/1*srl6X5xUrSH9lp0AvrX1DQ.png)

#### 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.](https://cdn-images-1.medium.com/max/375/1*jMpor_nqqsxHqAAftgKagg.png)

#### 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.](https://cdn-images-1.medium.com/max/379/1*idTzNgLCvxBQDkTHYHQSHQ.png)

#### 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.](https://cdn-images-1.medium.com/max/375/1*GFvBPnI7YnESw863RQAxBw.png)

#### 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.](https://cdn-images-1.medium.com/max/379/1*2PhGxPPG6Ub4pQdsVmLI2A.png)

#### toString()

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

![](https://cdn-images-1.medium.com/max/380/1*q84v3_dxoUt5B8F_9PSnUw.png)

#### 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’].](https://cdn-images-1.medium.com/max/383/1*xraX0n_h6RyrRMgd7vPzPQ.png)

#### charCodeAt()

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

![](https://cdn-images-1.medium.com/max/379/1*FwJYRFF3DdZXYjnVsfd5Bg.png)

#### 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.](https://cdn-images-1.medium.com/max/383/1*AVyvBdUrrkST6SC6cJC7rA.png)

#### 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’].](https://cdn-images-1.medium.com/max/530/1*h0uWZCVv-oA5IVSxWKJzrg.png)

#### 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](https://medium.com/u/984a2bedfdfc) JavaScript string methods cheat sheet I saw a while ago

. You can also take a look at [JavaScript.info](https://javascript.info/) to learn more

. Also, [w3schools](https://www.w3schools.com/jsref/jsref_obj_string.asp) is also a great resource to learn about strings in-depth.