Strings are pieces of text or “strings” of characters that are wrapped in quotes like so:
“Holidays” or ‘Holiday’. You can use single quotes or double quotes, it performs the same job. In the console, you can check
typeof “Holidays” and it will return “String”, similarly, if you type
Every character of a string has a corresponding index, starting at the 0 index, similar to an array object and we can access any character by its index. For example, if we have a string:
const holiday = “Christmas”
To access any character from it, we can simply type:
holiday > “t” holiday > “C” holiday > undefined // because there is no corresponding character at that index position.
To check the length of a string, you can use the
holiday.length > 9
One thing you need to be careful with here is that
.length will also count any spaces or symbols as well inside the string. Let's take a look at another example:
const fullName = “Uma Manandhar!” fullName.length >14
One might think it would return 12 but it actually returns 14 because it counts the spaces between first and last name and then the exclamation symbol as well. We can also concatenation two strings to one full string like so:
const firstName = “Aiden” const lastName = “Manandhar” const fullName = firstName + “ “ + lastName > "Aiden Manandhar"
In the above snippet we declared firstName and lastName variables, then we use the concatenate method(
+ symbol ) to set the fullName variable. Notice the empty string between firstName and lastName, this is there to add a space between them, without adding an empty string it would return
"AidenManadhar", which is probably not the format we would want to have in our application.
There are plenty of built-in methods for the String type which you can find in this document but here we will discuss some popular ones:
const currentHoliday = “Christmas” currentHoliday.toUpperCase() // converts string to UPPERCASE > “CHRISTMAS”
const nextHoliday = “NEW YEAR” nextHoliday.toLowerCase() // converts string to lowercase. > “new year”
const greeting = “HelloWorld” greeting.indexOf(“Hello”) // find the index of the starting character. > 0 greeting.indexOf(“World”) > 5 greeting.indexOf(“world”) // case sensitive. Returns -1 when nothing is found. > -1
const game = “baseball” game.slice(4) // slices of existing string and give a piece of string >”ball” game.slice(12) //means not found >”” game.slice(0, 4) //starts at index 0 and end at index 3 >”base” game.slice(4, 8) //starts at index 4 and end at index 7 >”ball”
const phrase = “you are very very smart” phrase.replace(“smart”, “intelligent”) // it specify what you want to replace and what you want to replace with >"you are very intelligent" phrase.replace(“very”, “a”) >”you are a very smart” // if there is the same word more than once, it changes only the first one phrase.replace(“so”, “so so”) >"you are very very smart" //stays unchanged
String is immutable so if you want to have all those updated returned values you need to set it in a variable like below:
const phrase = “you are very very smart” const updatedPhrase = phrase.replace(“smart”, “intelligent”) updatedPhrase >"you are very intelligent" phrase >”you are very very smart”
Also, we can chain methods like so:
” you are a rockstar like your mom ”.replace(“mom”, “dad”).toUpperCase().trim() >"YOU ARE A ROCK STAR LIKE YOUR DAD"
Thank you for reading.