String Literals

"My dog has fleas."
'Vermonters have a hundred words for "snow".'

String Escapes

console.log("Roses are red,\nViolets are blue;\nCandy is sweet,\nAnd so are you.")

String Messages

A string understands lots of messages. Here are a few:

"drive" + "way"
'Java' + "Script"

"Bert's pal Ernie" + ' sings "Rubber Duckie"'

"titanic".toUpperCase()
"QUIETLY".toLowerCase()

"Java".repeat(10)

"banana".length

"berry".charAt(1)
"berry".charAt(0)
"apple"[3]

"banana".includes("nan")
"banana".endsWith("ana")

"blueberry".replace("blue", "black")

Try all of these out in node or the browser console!

Check out MDN String docs for more.

Slicing and Dicing

Every string is made of lots of other strings.

You can pull out parts of a string with the slice message.

// this means "slice from character 0 to character 4"
"blueberry".slice(0, 4) 

// this means "slice from character 4 to the end
"blueberry".slice(4)

These start and end numbers are called indexes (or indices if you're feeling fancy).

MDN: slice

String Indexing Explained

Humans like to start counting at 1, but computers like to start counting at 0.

This can be confusing, so here's a visualization to help explain it.

Think of the indexes as pointing at the spaces between characters, as in this diagram:

| B | L | U | E | B | E | R | R | Y |
0   1   2   3   4   5   6   7   8   9

So with this picture in your mind, slice...

Try various start and end values in the console and see what happens!

Characters

Q: A string is "a series of characters"... but what is a character?

A: a character is a number (or character code) that stands for a symbol.

symbol code name
A 65 capital A
B 66 capital B
Z 90 capital Z
_ 95 underscore
a 97 lowercase A
??? 10 newline

(Some characters stand for unprintable symbols like newline or tab or bell.)

ASCII and ye shall receive-ski

ASCII Table

(image from Wikimedia Commons)

Unicode

Unicode Strings

JavaScript strings are Unicode

That means you can use emoji in your JavaScript programs!

Like this:

"😂".repeat(20)

Comparing Strings

JavaScript strings respond to the < and > operators.

> "apple" > "cherry"
false
> "banana" < "cherry"
true

Strings are compared one character at a time using the Unicode values of each character.

Comparing Strings: Example

So if you say "apple" < "apricot", JavaScript does something like this behind the scenes:

> "apple".charCodeAt(0)
97
> "apricot".charCodeAt(0)
97

> "apple".charCodeAt(1)
112
> "apricot".charCodeAt(1)
112

> "apple".charCodeAt(2)
112
> "apricot".charCodeAt(2)
114

In the above, 112 is less than 114, so the comparison stops there and returns true.

String Comparison Gotcha

In ASCII and Unicode, all the uppercase letters are together (codes 65 to 90), then all lowercase letters (codes 97 to 122).

That means that all uppercase strings are less than all lowercase strings.

> "apple" < "banana"
true
> "apple" < "BANANA"
false

The standard left-to-right code-to-code comparison algorithm is simplistic but very fast.

It works fine for many applications, but if you're dealing with user input or multiple languages...

Smart String Comparison

...use localeCompare instead, which understands case and diacriticals and dialects.

> "banana".localeCompare("CHERRY")
-1

Here -1 means "the left side is less than the right side". Try other comparisons and see what you get!

LAB: Strings: Exercises

 Previous Lesson Next Lesson 

Outline

[menu]

/