Clean and Check Text With String Methods

Every string you’ve compared in a condition so far, like "Saturday" in bus.js, was one you typed into the script yourself, exactly right. Text that other people type is rarely that neat.

When the day-trip sign-up form asks “Are you a club member?” one visitor types yes, another types Yes, and a third types YES with a stray space on each side. All three mean the same thing, but === compares text exactly, character by character, so only the first answer would match "yes".

In this lesson, you’ll clean typed answers before you compare them, using string methods, which are actions that every string in JavaScript can carry out. You’ll also check whether text contains some characters, copy part of a card number, and line up a small price list. The ticket checker at the end of this course gets its membership answer as typed text, so you’ll use this cleaning there.

Call a Method on a String

Start the Node REPL by running node in the terminal. The first line below stores an answer the way a visitor might type it, and the second compares it with "yes". The comparison is false for two reasons. The answer has a space at each end, and it starts with a capital Y.

Three string methods deal with those problems. The trim() method removes whitespace, meaning spaces, tabs, and linebreaks, from the start and the end of a string. The toLowerCase() and toUpperCase() methods change the letters to one case and leave everything else alone.

You call a method by writing a dot after the string, then the method’s name, then parentheses. Type these lines and watch what each one gives:

Language: JavaScript
> const answer = " Yes "
undefined
> answer === "yes"
false
> answer.trim()
'Yes'
> answer.toLowerCase()
' yes '
> answer.toUpperCase()
' YES '

trim() and toLowerCase() each fix one problem and leave the other. The REPL shows each result in quotes to remind you that it’s a string.

Now predict what the three methods give for a different answer, " nO ", then check in the REPL:

Language: JavaScript
> const other = "  nO "
undefined
> other.trim()
'nO'
> other.toLowerCase()
'  no '
> other.toUpperCase()
'  NO '

You’ve already used .length to read how many characters a string has. That’s a stored fact about the string, so it has no parentheses. A method does some work when you call it, and the parentheses are what make it run.

Note that trim() only touches the two ends. Whitespace between words stays, because it’s part of the text:

Language: JavaScript
> "  day  trip  ".trim()
'day  trip'

Keep the New String a Method Gives You

None of the string methods changes the string you call it on. Methods that transform text, like trim() and toLowerCase(), build a new string and give that back. Check it in the REPL:

Language: JavaScript
> const reply = " No "
undefined
> reply.trim()
'No'
> reply
' No '

After the call, reply still has both spaces, because JavaScript strings can’t be changed once they exist. Store the cleaned text in a variable of its own.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.