String Flashcards
Method returns a string created from the specified sequence of UTF-16 code units.
console.log(String.fromCharCode(189, 43, 190, 61)); // expected output: "½+¾="
Method returns a string created by using the specified sequence of code points.
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2F804)); // expected output: "☃★♲你"
Method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string.
var sentence = ‘The quick brown fox jumped over the lazy dog.’;
var index = 4;
console.log('The character at index ' + index + ' is ' + sentence.charAt(index)); // expected output: "The character at index 4 is q"
Method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
‘ABC’.charCodeAt(0); // returns 65
Method returns a non-negative integer that is the Unicode code point value.
‘ABC’.codePointAt(1); // 66
‘\uD800\uDC00’.codePointAt(0); // 65536
‘XYZ’.codePointAt(42); // undefined
Method concatenates the string arguments to the calling string and returns a new string.
var str1 = 'Hello'; var str2 = 'World';
console.log(str1.concat(' ', str2)); // expected output: "Hello World"
console.log(str2.concat(', ', str1)); // expected output: "World, Hello"
Method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.
const str1 = ‘Cats are the best!’;
console.log(str1.endsWith('best', 17)); // expected output: true
const str2 = ‘Is this a question’;
console.log(str2.endsWith('?')); // expected output: false
Method determines whether one string may be found within another string, returning true or false as appropriate.
var str = ‘To be, or not to be, that is the question.’;
console. log(str.includes(‘To be’)); // true
console. log(str.includes(‘question’)); // true
console. log(str.includes(‘nonexistent’)); // false
console. log(str.includes(‘To be’, 1)); // false
console. log(str.includes(‘TO BE’)); // false
Method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
var str = ‘Brave new world’;
console. log(‘Index of first w from start is ‘ + str.indexOf(‘w’)); // logs 8
console. log(‘Index of “new” from start is ‘ + str.indexOf(‘new’)); // logs 6
Method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.
var anyString = ‘Brave new world’;
console.log(‘The index of the first w from the beginning is ‘ + anyString.indexOf(‘w’));
// logs 8
console.log(‘The index of the first w from the end is ‘ + anyString.lastIndexOf(‘w’));
// logs 10
console.log(‘The index of “new” from the beginning is ‘ + anyString.indexOf(‘new’));
// logs 6
console.log(‘The index of “new” from the end is ‘ + anyString.lastIndexOf(‘new’));
// logs 6
Method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
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.
var a = 'réservé'; // with accents, lowercase var b = 'RESERVE'; // no accents, uppercase
console.log(a.localeCompare(b)); // expected output: 1 console.log(a.localeCompare(b, 'en', {sensitivity: 'base'})); // expected output: 0
Method retrieves the matches when matching a string against a regular expression.
var paragraph = 'The quick brown fox jumped over the lazy dog. It barked.'; var regex = /[A-Z]/g; var found = paragraph.match(regex);
console.log(found); // expected output: Array ["T", "I"]
Method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
‘abc’.padEnd(10); // “abc “
‘abc’.padEnd(10, “foo”); // “abcfoofoof”
‘abc’.padEnd(6, “123456”); // “abc123”
‘abc’.padEnd(1); // “abc”
Method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start (left) of the current string.
‘abc’.padStart(10); // “ abc”
‘abc’.padStart(10, “foo”); // “foofoofabc”
‘abc’.padStart(6,”123465”); // “123abc”
‘abc’.padStart(8, “0”); // “00000abc”
‘abc’.padStart(1); // “abc”
Method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.
'abc'.repeat(-1); // RangeError 'abc'.repeat(0); // '' 'abc'.repeat(1); // 'abc' 'abc'.repeat(2); // 'abcabc' 'abc'.repeat(3.5); // 'abcabcabc' (count will be converted to integer) 'abc'.repeat(1/0); // RangeError
({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2); // 'abcabc' (repeat() is a generic method)