Methods
Last updated
Last updated
Like other object, the String object has many built-in methods and functions that you can access. Here are some of the most common:
Name
Description
length
This string property returns the length of the string.
charAt()
Returns the character at the given index of the string. If left blank, returns the character at index 0.
concat()
Takes an unlimited number of strings, concatenates them to the original string, and returns the result.
endsWith():
Returns true
if the given character is at the last index of the string, otherwise returns false
.
includes()
Returns true
if the given character is present in the string, otherwise returns false
.
indexOf()
Returns the first index location of the given character in the string if present. Returns -1 if not found.
lastIndexOf()
Returns the last index location of the given character in the string if present. Returns -1 if not found.
repeat()
Duplicates the original string the given number of times and returns the result.
slice()
Cuts the string in two at the given index and returns the result.
startsWith()
Returns true
if the given character is at the first index of the string, otherwise returns false
.
toUpperCase()
Capitalizes each letter in the original string and returns the result.
toLowerCase()
Converts each letter in the original string to lower case and returns the result.
trim()
Removes white space at either end of the original string and returns the result.
You can find more information on these and other String methods
We will be working in the following file:
Create a string and print it to the console, print the length of the string, then remove the last two characters of the string and print the result.
Add the string "HELLO WORLD"
to your current string at index 3 and print the result.
Create one variable with the string EFA JS March 2018
and a second variable that is a blank string. Split the string at the M in March, put the result into the second variable, and print both to the console.
Create a string with your first name. Write a function to capitalize only the first letter and print the result to the console.
Create a string with your first and last name. Write a function to capitalize only the first letter of each and return the result.