toUpperCase() and toLowerCase()

toUpperCase() and toLowerCase()

Part 0 of the Series

·

1 min read

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: