Top 10 JavaScript String Methods You Should Know

Arun RArun R
4 min read

In JavaScript String methods help you to work with strings having a good knowledge of those methods helps you while working with strings in your JavaScript, in this article we will discuss on Top 10 JavaScript String Methods You Should Know

Types of String methods used in Javascript :

length Method

The length method is one of the basic and important methods in javascript. The length method is used to return the length of the string.

const str= 'JAVASCRIPT'
console.log(str.length)

OUTPUT:

10

toUpperCase( )

Consider we have a string called Javascript, While working with string we will come across a few properties and lots of methods among that one of the properties is very important that is called length which will show you the total number of characters that are present in a string While working in Javascript if we need to invoke a method we need to use round brackets, Here toUpperCase( ) method used to return a string converted into an upper case.

const str= 'javascript'
console.log(str.length)
console.log(str.toUpperCase())

OUTPUT:

10

JAVASCRIPT

toLowerCase( )

Similar to the upper case javascript uses a string method called as toLowerCase( ) where it executes the operations and helps to convert a string into a lowercase.

const str= 'JAVASCRIPT'
//console.log(str.length)
//console.log(str.toUpperCase())
console.log(str.toLowerCase())

OUTPUT:

javascript

indexOf( )

In javascript if you want to find a certain character is present in the string we use a method called as indexOf() which returns the index of the first occurrence, indexOf is going to scan from left to right, If you are looking for a character that is not present in a string it returns -1, Let's understand it with an example.

most str= 'javascript'
//console.log(str.length)
//console.log(str.toUpperCase())
//console.log(str.toLowerCase())
console.log(str.indexOf('a'))
console.log(str.indexOf('b'))

OUTPUT:

1

-1

includes( )

Another way to determine whether several characters are present in a string or not, To find it we can use a method called includes(), includes() is a boolean method that returns true/false if the string contains the specified str/chars.

const str= 'javascript'
//console.log(str.length)
//console.log(str.toUpperCase())
//console.log(str.toLowerCase())
//console.log(str.indexOf('a')) // 1
//console.log(str.indexOf('b')) //-1
console.log(str.includes('a')) // true
console.log(str.includes('n')) // false

OUTPUT:

true

false

lastIndexOf()

lastIndexOf() method is the same as the indexOf() method where the index of scans from the first occurrence to the last occurrence ( Left to right ), Whereas lastIndexOf() scans and returns the index of the last occurrence (right to left) and sends the result in which the particular character is found.

const str= 'javascript'
//console.log(str.length)
//console.log(str.toUpperCase())
//console.log(str.toLowerCase())
//console.log(str.indexOf('a')) // 1
//console.log(str.indexOf('b')) //-1
//console.log(str.includes('a')) // true
//console.log(str.includes('n')) // false
console.log(str.lastIndexOf('a')) //3

OUTPUT :

3

charAt( )

charAt( ) is an inbuilt method that returns characters at a specified index If you are using via index you need to use over square bracket [ ], But when you are using via charAt() you need to use round brackets( )

const str= 'javascript'
//console.log(str.length)
//console.log(str.toUpperCase())
//console.log(str.toLowerCase())
//console.log(str.indexOf('a')) // 1
//console.log(str.indexOf('b')) //-1
//console.log(str.includes('a')) // true
//console.log(str.includes('n')) // false
//console.log(str.lastIndexOf('a')) //3
console.log(str[1]) // ' a '
console.log(str.charAt(1)) // 'a'

OUTPUT:

a

a

slice(begin,end)

Let's initialize a keyword const and name it and the slice will return the extracted part in a new string.

const name = 'sachin'
console.log(name.slice(0,4))
console.log(name)

OUTPUT:

sach

slice(begin):

In javascript slice(begin) method will slice out the rest of the string, Check out the example below

const name = 'sachin'
console.log(name.slice(0,4))
console.log(name)
console.log(name.slice(1))

OUTPUT

achin

Capitalize a word using string methods:

In javascript there is no way of capitalizing a word or string, we use charAt(), upper and lower case and slice methods to perform the operations

const str= 'javascript'
//console.log(str.length)
//console.log(str.toUpperCase())
//console.log(str.toLowerCase())
//console.log(str.indexOf('a')) // 1
//console.log(str.indexOf('b')) //-1
//console.log(str.includes('a')) // true
//console.log(str.includes('n')) // false
//console.log(str.lastIndexOf('a')) //3
//console.log(str[1]) // ' a '
//console.log(str.charAt(1)) // 'a'

const name = 'sachin'
console.log(name.slice(0,4))
console.log(name)
console.log(name.slice(1))
console.log(name.charAt(0).toUpperCase() + name.slice(1).toLowerCase())

OUTPUT:

Sachin

Conclusion:

As we discussed the above-mentioned are some of the most important JavaScript String Methods you should be aware of, In our upcoming articles we will go through the Javascript Array Methods in detail

Also, read our article: https://blog.skillsafari.in/javascript-number-methods-you-should-know-in-2023

3
Subscribe to my newsletter

Read articles from Arun R directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arun R
Arun R