String Flashcards Preview

JavaScript FH > String > Flashcards

Flashcards in String Deck (33):
1

Method returns a string created from the specified sequence of UTF-16 code units.

console.log(String.fromCharCode(189, 43, 190, 61));
// expected output: "½+¾="

2

Method returns a string created by using the specified sequence of code points.

console.log(String.fromCodePoint(9731, 9733, 9842, 0x2F804));
// expected output: "☃★♲你"

3

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"

4

Method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

'ABC'.charCodeAt(0); // returns 65

5

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

6

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"

7

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

8

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

9

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

10

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

11

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

12

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"]

13

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"

14

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"

15

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)

16

Method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.

var str = 'Twas the night before Xmas...';
var newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr); // Twas the night before Christmas...

17

Method executes a search for a match between a regular expression and this String object.

var str = "hey JudE";
var re = /[A-Z]/g;
var re2 = /[.]/g;
console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"
console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation

18

Method extracts a section of a string and returns it as a new string, without modifying the original string.

var str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30);
console.log(str2); // OUTPUT: he morn
console.log(str3); // OUTPUT: morning is upon u
console.log(str4); // OUTPUT: is upon us.
console.log(str5); // OUTPUT: ""

19

Method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

function splitString(stringToSplit, separator) {
var arrayOfStrings = stringToSplit.split(separator);

console.log('The original string is: "' + stringToSplit + '"');
console.log('The separator is: "' + separator + '"');
console.log('The array has ' + arrayOfStrings.length + ' elements: ' + arrayOfStrings.join(' / '));
}

var tempestString = 'Oh brave new world that has such people in it.';
var monthString = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';

var space = ' ';
var comma = ',';

splitString(tempestString, space);
splitString(tempestString);
splitString(monthString, comma);

20

Method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

//startswith
var str = 'To be, or not to be, that is the question.';

console.log(str.startsWith('To be')); // true
console.log(str.startsWith('not to be')); // false
console.log(str.startsWith('not to be', 10)); // true

21

Method returns the part of the string between the start and end indexes, or to the end of the string.

var str = 'Mozilla';

console.log(str.substring(1, 3));
// expected output: "oz"

console.log(str.substring(2));
// expected output: "zilla"

22

Method returns the calling string value converted to lower case, according to any locale-specific case mappings.

'ALPHABET'.toLocaleLowerCase(); // 'alphabet'

'\u0130'.toLocaleLowerCase('tr') === 'i'; // true
'\u0130'.toLocaleLowerCase('en-US') === 'i'; // false

let locales = ['tr', 'TR', 'tr-TR', 'tr-u-co-search', 'tr-x-turkish'];
'\u0130'.toLocaleLowerCase(locales) === 'i'; // true

23

Method returns the calling string value converted to upper case, according to any locale-specific case mappings.

'alphabet'.toLocaleUpperCase(); // 'ALPHABET'

'Gesäß'.toLocaleUpperCase(); // 'GESÄSS'

'i\u0307'.toLocaleUpperCase('lt-LT'); // 'I'

let locales = ['lt', 'LT', 'lt-LT', 'lt-u-co-phonebk', 'lt-x-lietuva'];
'i\u0307'.toLocaleUpperCase(locales); // 'I'

24

Method returns the calling string value converted to lower case.

console.log('ALPHABET'.toLowerCase()); // 'alphabet'

25

Method returns a string representing the specified object.

var x = new String('Hello world');

console.log(x.toString()); // logs 'Hello world'

26

Method returns the calling string value converted to uppercase (the value will be converted to a string if it isn't one).

console.log('alphabet'.toUpperCase()); // 'ALPHABET'

27

Method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

var greeting = " Hello world! ";

console.log(greeting);
// expected output: " Hello world! ";

console.log(greeting.trim());
// expected output: "Hello world!";

28

Method removes whitespace from the end of a string. trimRight() is an alias of this method.

var greeting = " Hello world! ";

console.log(greeting);
// expected output: " Hello world! ";

console.log(greeting.trimEnd());
// expected output: " Hello world!";

29

Method removes whitespace from the beginning of a string.

var greeting = " Hello world! ";

console.log(greeting);
// expected output: " Hello world! ";

console.log(greeting.trimStart());
// expected output: "Hello world! ";

30

Method returns the primitive value of a String object.

var x = new String('Hello world');
console.log(x.valueOf()); // Displays 'Hello world'

31

Method returns a new Iterator object that iterates over the code points of a String value, returning each code point as a String value.

var str = 'A\uD835\uDC68';

var strIter = str[Symbol.iterator]();

console.log(strIter.next().value); // "A"
console.log(strIter.next().value); // "\uD835\uDC68"

32

Method is a tag function of template literals, similar to the r prefix in Python or the @ prefix in C# for string literals (yet there is a difference: see explanations in this issue). It's used to get the raw string form of template strings, that is, substitutions (e.g. ${foo}) are processed, but escapes (e.g. \n) are not.

String.raw`Hi\n${2+3}!`;
// 'Hi\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.

33

Method returns the Unicode Normalization Form of a given string (if the value isn't a string, it will be converted to one first).

var first = '\u212B'; // "Å"
var second = '\u0041\u030A'; // "Å"

console.log(first + ' and ' + second + ' are' +
((first === second)? '': ' not') + ' the same.');
// expected output: "Å and Å are not the same."

console.log(first + ' and ' + second + ' can' +
((first.normalize('NFC') === second.normalize('NFC'))? '': ' not') + ' be normalized');
// expected output: "Å and Å can be normalized"

var oldWord = 'mañana';
var newWord = oldWord.normalize('NFD');
console.log('The word did ' + ((oldWord != newWord)? 'not ' : '') + 'change.');
// expected output: "The word did change."