I realized I forgot toUpperCase() and toLowerCase(), so here's Part 0 of JavaScript String Methods!
The JavaScript String Methods series
- You are herePart 0 - toUpperCase() and toLowerCase()
- Part 1 - split() and join()
- Part 2 - slice(), substring(), substr()
- Part 3 - charAt(), charCodeAt(), fromCharCode(), at()
- Part 4 - concat() and repeat()
- Part 5 - search() and includes()
- Part 6 - indexOf() and lastIndexOf()
- Part 7 - endsWith() and startsWith()
- Part 8 - match() and matchAll()
- Part 9 - replace() and replaceAll()
- Part 10 - trim(), trimEnd() and trimStart()
- Part 11 - padStart() and padEnd()
In this post, skip to:
On Strings
Strings hold data in text form. They have a length
property, and you can use +
and +=
to easily concatenate them:
let firstString = 'Hi'
let secondString = 'there!'
firstString.length
//output: 2
secondString.length
//output 6
firstString += ' you'
//output: 'Hi you'
firstString.length
//output: '6'
firstString + secondString
//output: 'Hi youthere!'
thirdString = firstString + ' ' + secondString
//output: 'Hi you there!'
thirdString.length
//output: 13
toUpperCase
toUpperCase()
will return a new string after converting the value of the original to uppercase, if possible.
let firstString = 'Hi'
let secondString = 'there!'
let notAString = true
firstString.toUpperCase() + ' you'
//output: 'HI you'
thirdString = firstString + ' ' + secondString
//output: 'Hi you there!'
thirdString.toUpperCase()
//output: 'HI YOU THERE!'
notAString.toUpperCase()
//output: Uncaught TypeError
notAString.toString().toUpperCase()
//output: 'TRUE'
toLowerCase
toLowerCase()
goes the opposite way, converting the value to lowercase and then returning the new string.
let firstString = 'Hi'
let secondString = 'ThERe!'
let thirdString = firstString + ' ' + secondString
//output: 'Hi ThERe!'
let fourthString = 'hi there!'
fourthString === thirdString
//output: false
thirdString.toLowerCase()
//output: 'hi there!'
fourthString === thirdString.toLowerCase()
//output: true
Practice
If you want a little practice, these 8-kyu Kata are a nice starting point: