What is a String in JavaScript?
In JavaScript, a string is used to store text. You can create a string by wrapping the text in:
Single quotes
' '
Double quotes
" "
Backticks
` `
Examples of String Literals:
You can store a string in a variable using the =
sign:
Template Strings
Template strings (also called template literals) use backticks (`
). They let you:
Insert variables and expressions with
${ }
Write multi-line strings
Example:
Multi-line Template String:
Multi-line strings don’t work with ' '
or " "
.
Multi-line strings don’t work with ' '
or " "
.
Strings Like Arrays
You can treat a string like an array of characters.
Example:
But note: you can’t change characters like this:
Loop Through a String
Use loops to go through each character:
Quotes Inside Strings
You can use single quotes inside double-quoted strings and vice versa:
To include the same type of quote, use a backslash \
:
String Concatenation (Joining Strings)
You can combine strings using +
or .concat()
:
String Objects vs. String Literals
You can also create strings as objects:
Comparing Strings
You can compare strings using:
<
,>
,==
,===
.localeCompare()
Examples:
Note: ===
checks type and value, so it returns false
when comparing a string object to a string literal.
Common String Properties and Methods
Property:
length
– Returns number of characters
length
– Returns number of characters
Useful Methods:
Method Description charAt(index)
Get character at position charCodeAt(index)
Get Unicode value of character concat(str)
Combine strings indexOf(str)
First position of substring lastIndexOf(str)
Last position of substring slice(start, end)
Get part of string substring(start, end)
Similar to slice substr(start, length)
Get part of string by length replace(old, new)
Replace text split(separator)
Split into array toUpperCase()
All uppercase toLowerCase()
All lowercase valueOf()
Get primitive string value toString()
Converts to string
Examples for each of the above listed JavaScript string methods:1. charAt(index)
– Get character at positionlet str = "Hello";console.log(str.charAt(1)); // "e"
Method | Description |
---|---|
charAt(index) | Get character at position |
charCodeAt(index) | Get Unicode value of character |
concat(str) | Combine strings |
indexOf(str) | First position of substring |
lastIndexOf(str) | Last position of substring |
slice(start, end) | Get part of string |
substring(start, end) | Similar to slice |
substr(start, length) | Get part of string by length |
replace(old, new) | Replace text |
split(separator) | Split into array |
toUpperCase() | All uppercase |
toLowerCase() | All lowercase |
valueOf() | Get primitive string value |
toString() | Converts to string |
charAt(index)
– Get character at position2. charCodeAt(index)
– Get Unicode value of character
let str = "A";console.log(str.charCodeAt(0)); // 65
3. concat(str)
– Combine strings
4. indexOf(str)
– First position of substring
5. lastIndexOf(str)
– Last position of substring
6. slice(start, end)
– Get part of string
7. substring(start, end)
– Similar to slice
8. substr(start, length)
– Get part of string by length
Deprecated but still works in most browsers.
9. replace(old, new)
– Replace text
10. split(separator)
– Split into array
11. toUpperCase()
– All uppercase
12. toLowerCase()
– All lowercase
13. valueOf()
– Get primitive string value
14. toString()
– Converts to string
These methods wrap your string in HTML tags:
Method | HTML Tag |
---|---|
bold() | <b> |
italics() | <i> |
anchor(name) | <a name=""> |
fontcolor(color) | <font color=""> |
fontsize(size) | <font size=""> |
strike() | <strike> |
sub() | <sub> |
sup() | <sup> |