'
) or double quotes ("
)'My dog has fleas'
"Vermont has a hundred words for 'snow'"
\n
) means newline
console.log('Roses are red,\nViolets are blue;\nCandy is sweet,\nAnd so are you.')
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.'
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.
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.
"Hello".repeat(3).toUpperCase()
.replaceAll(subString, newValue)
'\n'
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.
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)
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.
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
ortab
Credit Wikimedia Commons)
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
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.
Which of these Strings is considered greater than the other?
'apple' > 'apricot'
Think through this comparison first, then check it in JavaScript.
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
.
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
/