L1: String Methods, for while loops Flashcards
What does String.prototype.concat() mutate/return?
Returns new string, basically the same as going caller + argument
Can .concat take multiple arguments
yes
basically argument + argument + argument
What does String.prototype.includes() mutate/return?
Returns boolean if the caller contains the substring
What does the optional second argument do in String.prototype.includes()
Indicates with index to start looking from
What does String.prototype.split() do if there’s no argument?
Just gives the full string in an array.
What does String.prototype.trim() mutate/return?
returns string with caller’s whitespaces, newlines \n, and tabs \t remove
method to only remove spaces from beginning or end
.trimStart()
.trimEnd()
Return boolean if a string contain a substring
.includes()
Return the character at a certain index of a string (2 ways)
.charAt(2)
or
‘string’[2]
What’s the difference between .charAt and ‘string’[2]in regard to whether the character exists
‘string’[2] will return undefined if that index doesn’t exist
.charAt will return an empty string.
Return the Unicode code point or character code for a given index of a string
‘abcdef’.charCodeAt(1)
Create a character for a given char code argument
String.fromCharCode(97)
Why are some methods prototypes and some not? like
String.fromCharCode(97)
‘a’
It’s a common pattern in different languages to write methods that don’t pertain to a specific value of a type directly on the class/constructor for that type. In this case, fromCharCode isn’t an operation you’d perform on a string value. That is, something like the following doesn’t make sense:
‘abcd’.fromCharCode(97)
Return boolean if a string ends or starts with a certain substring
String.prototype.endsWith
String.prototype.startsWith
Return a string a number of times
String.prototype.repeat