let myString = 'This is my string'; let myOtherString = "This is my string too";
There is no difference between using single or double quotes however you may get some different opinions on why each is better.
.length is a string property that returns the length of a string. Properties describe an object and in this case describe out string.
let myString = 'bottle'; myString.length; // 6
Strings also have methods that can be called on string
Methods or functions require parenthesis
.toLowerCase() because they are actions performed on objects. For strings,
.length is already computed so it doesn't need the parenthesis but
.toLowerCase() does because it is an action on
the string object.
let myString = 'This is my string'; myString.includes('my') // true myString.includes('yours') // false
.toUpperCase() and .toLowerCase()
let myString = 'This Is My String'; myString.toUpperCase() // THIS IS MY STRING myString.toLowerCase() // this is my string
To get a single character from a string, use square brackets
. Strings are 0-indexed meaning that when counting the
position of characters you start at 0, not 1.
let myString = 'This Is My String'; myString // T myString // h myString // i
We can use the
.length property of string to help access characters. For example if you wanted to access the last character
of a string:
let myString = 'This Is My String'; myString[myString - 1] // g
A substring is a part of a string: If '
substring' is a string,
'sub' is a substring to
'substring' (say that fast 5 times).
Instead of accessing each character of the substring you want, you can use the function
.substring(indexStart, indexEnd) to help you.
let myString = 'This Is My String'; myString.substring(1, 3) // his myString.substring(myString.length - 3, myString.length - 1) // ing
indexEnd is an optional parameter of .substring() meaning that it is not required to call the function on a string
let myString = 'This Is My String'; myString.substring(myString.length - 3) // ing myString.substring(myString.length - 6) // String
.endsWith() are also useful functions for substrings. The names are self explanatory but just in case... they check if a string starts
or ends with a specific substring.
let myString = 'This Is My String'; myString.startsWith('This') // true myString.startsWith('his') // false myString.endsWith('Str') // false myString.endsWith('String') // true
Sometimes we get nasty string that have unexpected spaces at the start or end.
.trim() removed additional white spaces
from the beginning, end, or both!
let myString = ' This Is My String '; let myOtherString = ' This Is My Other String'; myString.trim() // `This Is My String` myOtherString.trim() // `This Is My Other String`
+. Concatenation just means merging or combining the strings.
let myString = 'This is '; let otherString = 'My String'; myString + otherString // 'This Is My String'
You can create a template string by using backticks, ` , instead of single or double quotes.
`Look I'm on multiple lines!`