JavaScript String Methods Flashcards

1
Q

charCodeAt()

A

Returns the Unicode of the character at a specified index

let word = “Hello world”
let letter = word.charAt(0)
console.log(letter)

Output:
72

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

CharAt()

A

Returns the character at a specified index (position)

let word = “Hello world”
let letter = word.charAt(0)
console.log(letter)

OUTPUT:
H

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

concat()

A

Returns two or more joined strings

let wordOne = “Hello”;
let wordTwo= “World”;
let total = wordOne.concat(wordTwo);
console.log(total)

OUTPUT:
HelloWorld

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

endsWith()

A

Returns True or False if a string ends with a specified value

let phrase = “Hello World”;
let word = phrase.endsWith(“World”)
console.log(word)

OUTPUT:
True

let word = phrase.endsWith(“rld”)
console.log(word)

OUTPUT:
True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

fromCharCode()

A

Returns Unicode values as characters

let word = String.fromCharCode(72, 69, 76, 76, 79);
console.log(word);

OUTPUT:
HELLO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

includes()

A

Returns if a string contains a specified value

let sentence = “Hi, my name is Messi”;
let answer = sentence.includes(“Messi”)
console.log(answer)

OUTPUT:
true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

indexOf()

A

Returns the index (position) of the first occurrence of a value in a string

let sentence = “Hello world, how are you world?”
let answer = “sentence.indexOf(“world”)
console.log(answer)

OUTPUT:
6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

lastIndexOf()

A

Returns the index (position) of the last occurrence of a value in a string.

let sentence = “Hello world, how are you world?”
let answer = sentence.lastIndex(“world”)
console.log(answer)

OUTPUT:
25

let answer = sentence.lastIndexOf(“WORLD”)
console.log(sentence)

OUTPUT:
-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

localeCompare()

A

Compares two strings in the current locale.
Returns an integer indicating whether the reference Str comes before, after, or is equivalent to the compareString.
-1 if sorted before, 1 if sorted after, 0 of equal.

let strOne = “ab”
let strTwo = “cd”
let answer = strOne.localeCompare(strTwo)

OUTPUT:
-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

match()

A

Searches a string for a value, or a regular expression, and returns the matches.
If no match, it returns nothing.

let sentence = “I love to play soccer”
let answer = sentence.match(“soccer”)
console.log(answer)

OUTPUT:
soccer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

repeat()

A

Returns a new string with a number of copies of a string

let sentence = “Hello world”;
let answer = sentence.repeat(3)
console.log(answer)

OUTPUT:
Hello worldHello worldHello world

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

replace()

A

Searches a string for a value, or a regular expression, and returns a string where the values are replaced

let sentence = “Hello world”;
let answer = sentence.replace(“world”, “Earth”)
console.log(answer)

OUTPUT:
Hello Earth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

search()

A

Searches a string for a value, or regular expression, and returns the index (position) of the match.

let sentence = “I love soccer”
let answer “ sentence.search(“soccer”)
console.log(answer)

OUTPUT:
7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

slice()

A

Extracts part of a new string and returns part of a new string

let sentence = “I like to play soccer.”;
let answer = sentence.slice(0, 6)
console.log(answer)

let answerTwo = sentence.slice(-1);
console.log(answerTwo);
let answerThree = sentence.slice(0);
console.log(answerThree);

OUTPUT:
I like

.

I like to play soccer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

split()

A

Splits a string into an array of substrings

The split() method does not change the original string.

let sentence = “Hello world.”;
let answer = sentence.split(“ “)
console.log(answer);

OUTPUT:
[“Hello”, “world”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

startsWith()

A

Checks whether a string begins with a specified character

string.startsWith(searchValue, start)

let sentence = “My favorite sport is soccer.”
let answer = sentence.startsWith(“soccer”)
console.log(answer)

OUTPUT:
false

17
Q

substr()

A

Extracts a number of characters from a string, from a start index (position)

string.substr(start, length)
Length is optional

let sentence = “I love Liverpool FC”
let word = sentence.substr(7, 9)
console.log(word)

OUTPUT:
Liverpool

18
Q

substring()

A

Extracts characters from a string, between two specified indices (positions)

string.substring(start, end)
end is optional

let sentence = “I love Liverpool FC”
let word = sentence.substring(2, 6)
console.log(word)

OUTPUT:
love

19
Q

toLocaleLowerCase()

A

Returns a string converted to lowercase letters, using the host’s locale

let sentence = “I love Liverpool FC”;
let answer = sentence.toLocaleLowerCase();
console.log(answer)

OUTPUT:
I love liverpool fc

20
Q

trim()

A

Returns a string with removed whitespaces

let word = “ Hello world “;
let answer = word.trim();
console.log(answer);

OUTPUT:
Hello world

21
Q

toLocaleUpperCase()

A

Returns a string converted to uppercase letters, using the host’s locale

let sentence = “I love Liverpool FC”;
let answer = sentence.toLocaleUpperCase();
console.log(answer);

OUTPUT:
I LOVE LIVERPOOL FC

22
Q

toUpperCase()

A

Returns a string converted to uppercase letters

let sentence = “I love Liverpool FC”;
let answer = sentence.toUpperCase();
console.log(answer)

OUTPUT:
I LOVE LIVERPOOL FC

23
Q

toString()

A

Returns a string or a string object as a string

let sentence = “I love soccer”;
let answer = sentence.toString();
console.log(answer);

OUTPUT:
I love soccer
————————————————
let numbers = [7, 8, 9];
let answer = numbers.toString();
console.log(answer);

OUTPUT:
“7, 8, 9”

24
Q

toLowerCase()

A

Returns a string converted to lowercase letters

let sentence = “I love Liverpool FC”;
let answer = sentence.toLowerCase();
console.log(answer)

OUTPUT:
i love liverpool fc

25
Q

valueOf()

A

Returns the primitive value of a string or a string object

const stringObj = new String(‘foo’);

console. log(stringObj);
console. log(stringObj.valueOf());

OUTPUT:
String { “foo” }
“foo”