DEV Community

Cover image for JavaScript String Methods Cheatsheet
Ijash
Ijash

Posted on • Edited on

JavaScript String Methods Cheatsheet

Here's a cheatsheet of the latest ECMAScript string methods in Markdown format:


length

string.length

Returns the number of characters in a string.

'Hello World'.length
Enter fullscreen mode Exit fullscreen mode

Results: 11

toUpperCase()

String.prototype.toUpperCase()

Converts all characters in a string to uppercase.

'hello'.toUpperCase()
Enter fullscreen mode Exit fullscreen mode

Results: 'HELLO'

toLowerCase()

String.prototype.toLowerCase()

Converts all characters in a string to lowercase.

'HELLO'.toLowerCase()
Enter fullscreen mode Exit fullscreen mode

Results: 'hello'

indexOf()

String.prototype.indexOf(searchValue: string, fromIndex?: number)

Returns the index of the first occurrence of a specified value in a string.

'Hello World'.indexOf('World')
Enter fullscreen mode Exit fullscreen mode

Results: 6

slice()

String.prototype.slice(start?: number, end?: number)

Extracts a section of a string and returns it as a new string.

'Hello World'.slice(0, 5)
Enter fullscreen mode Exit fullscreen mode

Results: 'Hello'

replace()

String.prototype.replace(searchValue: string | RegExp, replacement: string | (substring: string, ...args: any[]) => string)

Replaces a specified value or pattern in a string with another value.

'Hello World'.replace('World', 'there')
Enter fullscreen mode Exit fullscreen mode

Results: 'Hello there'

split()

String.prototype.split(separator?: string | RegExp, limit?: number)

Splits a string into an array of substrings based on a specified separator.

'Hello World'.split(' ')
Enter fullscreen mode Exit fullscreen mode

Results: ['Hello', 'World']

trim()

String.prototype.trim()

Removes whitespace from both ends of a string.

'   Hello World   '.trim()
Enter fullscreen mode Exit fullscreen mode

Results: 'Hello World'

charAt()

String.prototype.charAt(index: number)

Returns the character at a specified index in a string.

'Hello'.charAt(0)
Enter fullscreen mode Exit fullscreen mode

Results: 'H'

concat()

String.prototype.concat(...strings: string[])

Concatenates the string arguments to the calling string and returns a new string.

'Hello'.concat(' ', 'World')
Enter fullscreen mode Exit fullscreen mode

Results: 'Hello World'


Sorting based on the most frequently used string methods, ordered accordingly

Top comments (0)