String Knowledge Flashcards
Why does this evaluate true?
‘z’ > ‘a’
Why is this false?
‘tb’ < ‘ta’
So strings become NaN when converted to a number. But with some comparisons, JavaScript uses the so-called “dictionary” or “lexicographical” order. It does this letter by letter (sort of like a number).
so z has a higher number than a
tb has a higher number than ta
“hello my name is stringy the whooping string”
but we want a function that will capitalize the first letter of each word
“Hello My Name Is Stringy The Whooping String”
With .split(), .map(), charAt(), .toUpperCase() and slice(). join()
Take the string
.split() it
.map() that array taking each word and using charAt(0), toUpperCase() it, and concatentate it with the rest of the word sliced.
Then join it.
OR
.split(), .map(), x[0].toUpperCase + x.subString
What happens with this code:
alert( ‘hello ${1}’ );
// hello 1
is alerted in browser
Evaluate:
‘Bee’ > ‘Be’
True
‘z’ > ‘aaaa’
True
It judges character from left to right. Returns whenever oneis larger than the other. it does not convert all characters into a single value.
How does the string > or < algorithm work
Compare the first character of both strings.
If the first character from the first string is greater (or less) than the other string’s, then the first string is greater (or less) than the second. We’re done.
Otherwise, if both strings’ first characters are the same, compare the second characters the same way.
Repeat until the end of either string.
If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.
Two benefits of backticks
- Allow embedded expressions
2. Allow string to span multiple lines
escape a backslash
\\
Add a tab in a string
\t
Add a unicode symbol or character
\xXX Unicode character with the given hexadecimal Unicode XX, e.g. ‘\x7A’ is the same as ‘z’.
\uXXXX A Unicode symbol with the hex code XXXX in UTF-16 encoding, for instance \u00A9 – is a Unicode for the copyright symbol ©. It must be exactly 4 hex digits.
\u{X…XXXXXX} (1 to 6 hex characters) A Unicode symbol with the given UTF-32 encoding. Some rare characters are encoded with two Unicode symbols, taking 4 bytes. This way we can insert long codes.
What is alerted:
alert( ‘My\n’.length );
// 3 \n is a single “special” character, so the length is indeed 3.
What is str at the end? let str = 'Hi'; str[0] = 'h';
Hi
strings are immutable
What is str at the end? let str = 'Hi'; str = 'h' + str[1];
hi
the variable is reassigned to something. The string did not change.
‘a’ < ‘Z’
‘a’ < ‘b’
‘a’ < ‘A’
false
true
false
Here is the number assigned to each character ascending: let str = '';
for (let i = 65; i <= 220; i++) { str += String.fromCodePoint(i); } alert( str );
Gives: // ABCDEFGHIJKLMNOPQRSTU VWXYZ[\]^_`abcdefghijklmn opqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜ
‘Österreich’ > ‘Zealand’
‘Osterreich’ > ‘Zealand’
true
false
let str = ‘’;
for (let i = 65; i <= 220; i++) { str += String.fromCodePoint(i); } alert( str ); // ABCDEFGHIJKLMNOPQRSTU VWXYZ[\]^_`abcdefghijklmn opqrstuvwxyz{|}~ // ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜ