Strings Flashcards
What are some examples of String literals?
‘String text’
“String text”
“Charactersinanylanguage”
How would you create a string using the String global object directly?
String(thing)
Parameters:
thing - anything to be converted to a string.
Since ECMAScript 2015, what can string literals be called?
Template strings
What is Escape notation and why use it?
Escape notation: \
Beside regular, printable characters, special characters can be encoded using escape notation.
Ex: \n for a new line. ' for a single quote
Does JavaScript differentiate between single quotes(‘’) and double quotes(“”)?
JavaScript makes no distinction between single or double quotes.
What two ways can you combine long literal strings, such as this question, to keep from having lines that go on endlessly?
You would use these techniques to make your code more readable.
Either use the + operator or a \ at the end of each line. The \ cannot have any spaces immediately after it.
Ex1: “What two ways can you combine long “ + “literal strings, such as this question, to “…”;
Ex2: “What two ways can you combine long \
literal strings, such as this question, to \ …”;
What are strings most useful for?
Strings are most useful for holding data that can be represented in text form.
You can use the operators + and += on strings?
True.
What are the two ways to access an individual character in a string?
charAt()
Ex: return ‘cat’.charAt(1); // returns “a”
‘someString’[1]
Ex: return ‘cat’[1]; // returns “a”
What is a String?
The String global object is a constructor for strings, or a sequence of characters.
What is the most common way to compare strings?
Using the less than operators.
Less common option but still valid:
A similar result can be achieved using the localeCompare() method inherited by String instances.
What does localeCompare() method do? and what is its syntax?
The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
Syntax:
referenceStr.localeCompare(compareString[, locales[, options]])
Example:
// The letter “a” is before “c” yielding a negative value
‘a’.localeCompare(‘c’); // -2 or -1
True / False:
JavaScript distinguishes between String objects and primitive string values.
True
What are string primitives?
String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive
True / False:
JavaScript does not automatically converts primitives to String objects, so that it’s possible to use String object methods for primitive strings
False
In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.
How do you convert a String Object to its primitive counterpart?
A String object can always be converted to its primitive counterpart with the valueOf() method.