JavaScript String Methods
It is often necessary to search inside and manipulate strings. So common are these tasks, that JavaScript has many different methods to help perform these tasks.
The following methods are some of the most commonly used methods when working with strings.
NOTE
Most string methods do NOT a change the original string, but instead return a new altered string.
toLowerCase() / toUpperCase()
It is often necessary to change the case of a string. JavaScript provide two easy to use methods for change the case of the letter, toLowerCase()
and toUpperCase()
. Like most string methods, these methods do not change the original string.
const greeting = 'Hello, World!'
// converting the letters to lowercase
console.log(greeting.toLowerCase()) // 'hello, world!'
// converting the letters to uppercase
console.log(greeting.toUpperCase()) // 'HELLO, WORLD!'
// The original string is unchanged
console.log(greeting) // 'Hello, World!'
split()
The split()
method splits a string into an array of strings, by separating the string on a specified separator.
const command = "Take me to your leader!"
// splitting the string at each space
console.log(command.split(' ')) // ['Take', 'me', 'to', 'your', 'leader!']