String Literals

'My dog has fleas'
"Vermont has a hundred words for 'snow'"

Escaping Strings

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

Lab: String Formatting

How could you format the following string to print in the format below?

'Roses are red, Violets are blue; Candy is sweet, And so are you.'

'Roses are red,
  Violets are blue;
    Candy is sweet,
      And so are you.'

String Messages

A string understands lots of messages.

Here are some of them.

'drive' + 'way'
'Java' + "Script"
'banana'.length
'titanic'.toUpperCase()
'QUIETLY'.toLowerCase()
'Hello'.concat(', world!')
'All dogs are good dogs'.replaceAll('dogs', 'puppers')
'Java'.repeat(10)
'berry'.charAt(0)
'berry'.charAt(1)
'banana'.includes('nan')
'banana'.endsWith('ana')
'blueberry'.replace('blue', 'black')

Try them out in an interactive node shell, or a JavaScript file in an editor.

Check out MDN String docs for more.


Lab: Combining Concepts

Practice combining operators and methods.

Given the following String, can you send messages to it to match the formatting below?

'this is a fantastic string'
THIS
IS
A
FANTASTIC
STRING

Remember the messages you saw in the prior slide.

Hint 1
You can chain multiple methods off of each other by calling them one after another. e.g.
"Hello".repeat(3).toUpperCase()
Hint 2
There is a message that can substitute all the `subString` values in a string with the `newValue`.
.replaceAll(subString, newValue)
Hint 3
The newline character can be used to create line breaks.
'\n'

String Slicing

You can extract sub-parts of a string using slice

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

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

The first number is which character to start from.

The second number is which character to end on.

MDN: slice


String Indexing

Indexes are the positions of characters within a String.

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

Then 'blueberry'.slice(0, 4)


Lab: Breaking Up Strings

Use the Starting String below, some operators, and multiple uses of .slice() to create the Target Final String.

Starting String

'strawberry banana milk shake'

Target Final String

'blueberry shake'

Hint: You can create variables using let to store sub-strings.


Characters

A string is composed of a sequence of characters.

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 symbols like newline or tab


ASCII Characters

ASCII Table

Credit Wikimedia Commons)


Unicode Characters


Unicode Strings

JavaScript strings are Unicode

That means you can use emoji in your JavaScript programs!

Like the following:

'😂'.repeat(10)
// '😂😂😂😂😂😂😂😂😂😂'

'😂'.codePointAt(0)
// 128514

String.fromCodePoint(128514)
// '😂'

This may not work by default in Windows Cmd or PowerShell


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.


Lab: Apple vs. Apricot

Which of these Strings is considered greater than the other?

'apple' > 'apricot'

Think through this comparison first, then check it in JavaScript.


Comparing Strings: Explanation

When comparing'apple' > 'apricot', JavaScript does this behind the scenes:

'apple'.charCodeAt(0) > 'apricot'.charCodeAt(0);
// value: 97

'apple'.charCodeAt(1) > 'apricot'.charCodeAt(1);
// value: 112

'apple'.charCodeAt(2) > 'apricot'.charCodeAt(2);
// value: 114

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


Comparing Strings: Cont

In both ASCII and Unicode, uppercase characters (codes 65 to 90) and lowercase letters (codes 97 to 122) are different.

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

> 'apple' < 'banana'
true
> 'apple' < 'BANANA'
false

 Previous Next 

Outline

[menu]

/