Strings Flashcards
(40 cards)
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.
How would you add a property to the String Object?
String.prototype
Allows the addition of properties to a String object.
What does the method String.formCharCode() do? And What is an example?
String.fromCharCode():
Returns a string created by using the specified sequence of Unicode values.
Example:
String.fromCharCode(65); // “A”
What does the property String.prototype.length do? And what is an example?
String.prototype.length
Reflects the length of the string.
Example:
‘String’.length; // 6
What does the method String.prototype.charAt() do? And What is an example?
String.prototype.charAt():
Returns the character at the specified index.
Example:
‘String’.charAt(4); // ‘n’
What does the method String.prototype.charCodeAt() do? And What is an example?
String.prototype.charCodeAt():
Returns a number indicating the Unicode value of the character at the given index.
Example:
‘String’.charCodeAt(2); // 114 (unicode value of r)
What does the method String.prototype.concat() do? And What is an example?
String.prototype.concat():
Combines the text of two strings and returns a new string.
Example:
‘String’.concat(‘ love’); // ‘String love’
What does the method String.prototype.includes() do? And What is an example?
String.prototype.includes():
Determines whether one string may be found within another string.
Example:
‘String’.includes(‘ring’); // true
‘Blue Whale’.includes(‘blue’); // false
What does the method String.prototype.endsWith() do? And What is an example?
String.prototype.endsWith():
Determines whether a string ends with the characters of another string.
Example:
‘String’.endsWith(‘ng’); // true
‘String’.endsWith(‘ri’); // false