“C” and “CODEDEC” are both treated as Strings. ” ” is also treated as Strings even if there is not any character inside the quotes.
- Here, variable a, b, and c are assigned String values in them.
- We can clearly see that the String values are closed in double quotes.
- In the terminal, we can see the output where the console.log() displays the values as Strings.
Working with Strings
WELCOME TO CODEDEC
- Here, we first declared the variable TestVariable using the keyword var.
- Then, we assigned a String using the assignment operator =.
- We stored a string value in the variable.
- Then we accessed the values inside the variable and using the document.write() method, the message is displayed.
We can also determine the length of a string using the property of the string: s.length
Accessing portion of a String
|s.substring(1,4)||“ell”: the 2nd, 3rd, and 4th characters|
|s.slice(1,4)||“ell”: the 2nd, 3rd, and 4th characters|
|s.slice(-3)||“rld”: last 3 characters|
|s.split(“, ”)||[“Hello”, “world”]: split at delimiter string|
Searching a String
|s.indexOf(“l”)||2: position of first letter l|
|s.indexOd(“l”, 3)||3: position of first “l” at or after 3|
|s.indexOf(“zz”)||-1: s does not include the substring “zz”|
|s.lastIndexOf(“l”)||10: position of last letter l|
Boolean search in ES6 and later
|s.startWith(“Hello”)||true: the string starts with these|
|s.endsWith(“!”)||false: s does not end with that|
|s.includes(“or”)||true: s includes substring “or”|
Modifying a String
|s.replace(“llo”, “y”)||“Hey, world”|
|s.normalize()||Unicode NFC normalization|
|s.normalize(“NFD”)||NFD normalization. Also “NFKC”, “NFKD”|
Inspecting individual character of a String
|s.charAt(0)||“H”: the first character|
|s.charAt(s.length-1)||“d”: the last character|
|s.charCodeAt(0)||72: 16-bit number at specified position|
|s.codePointAt(0)||72: works for codepoints|
|“x”.padStart(3)||“ x”: add spaces on the left to a length of 3|
|“x”.padEnd(3)||“x ”: add spaces on the right to a length of 3|
|“x”.padStart(3, “*”)||“**x”: add stars on the left to a length of 3|
|“x”.padEnd(3, “-”)||“X–”: add dashes on the right to a length of 3|
|“ test ”.trim()||“test”: remove spaces at start and end|
|“ test ”.trimStart()||“test ”: remove spaces on left (Also use trimLeft)|
|“ test ”.trimEnd()||“ test”: remove spaces on right (Also use trimRight)|
Other String function
|s.concat(“!”)||“Hello, world!”: (Also use + operator to do the same)|
|“<>”.repeat(5)||“<><><><><>”: concatenate n copies|
Strings can also be used as read-only arrays for accessing individual characters from a String using square brackets ‘[ ]’ instead of charAt() method.
Basic String Operations
Mixing Numbers and Strings
WELCOME TO THE YEAR 2021
- We can clearly see that the combination of String value and Number value is displayed in the output.
Comparison of Strings is done in the same way that we compare numbers but with only one major difference, i.e. keeping in mind that the String value will be compared alphabetically rather numerically.
- Here, the comparison is done by checking each letter in turn on the LHS and checks it with the letter in the same position on the RHS.
- If at any position, any character is found unmatched, the comparison stops and the result is false.
- If all the positions on both LHS and RHS have the same characters, then the result is true, and the flow control will enter the braces and execute the document.write() function.
Escape Sequences in String
- But, we have also included apostrophe at I’m and He’s, here the function will throw an error or print the wrong string value.
- And also we have included a backslash character(\) before the apostrophe at I\’m and He\’s, here the function will display the correct string value, i.e. I’m Tom and He’s Jerry.
Similarly like we have used a backslash character (\) in the above example to ignore the apostrophe, we can also use many other different Escape Sequences to avoid any error in the output of the String values.
Below is a table that briefly lists some escape sequences and the character they represent.
Used for Backspace
|Used for Form Feed|
Used for New Line
|Used for Carriage Return|
Used for Tab
|Used for Single Quote|
Used for Double Quote
|Used for Backslash|
NN is a hexadecimal number that specifies a character in Latin-1 character set