JavaScript String Methods Flashcards
charCodeAt()
Returns the Unicode of the character at a specified index
let word = “Hello world”
let letter = word.charAt(0)
console.log(letter)
Output:
72
CharAt()
Returns the character at a specified index (position)
let word = “Hello world”
let letter = word.charAt(0)
console.log(letter)
OUTPUT:
H
concat()
Returns two or more joined strings
let wordOne = “Hello”;
let wordTwo= “World”;
let total = wordOne.concat(wordTwo);
console.log(total)
OUTPUT:
HelloWorld
endsWith()
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
fromCharCode()
Returns Unicode values as characters
let word = String.fromCharCode(72, 69, 76, 76, 79); console.log(word);
OUTPUT:
HELLO
includes()
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
indexOf()
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
lastIndexOf()
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
localeCompare()
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
match()
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
repeat()
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
replace()
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
search()
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
slice()
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
split()
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”]