A JavaScript string is a primitive data type used to store text. Strings can be written using single quotes (' '), double quotes (" "), or backticks (` `).
A string is a sequence of characters enclosed in quotes.
Strings can be assigned to variables.
Template strings use backticks (` `) and allow variables or expressions to be inserted using ${ }.
Template strings can also span multiple lines.
A string can be accessed like an array.
Strings are immutable, so characters cannot be changed directly.
You can use single quotes inside double quotes and vice versa.
Strings can be joined using the + operator or concat() method.
JavaScript allows you to create string objects using the new String() constructor.
String objects and string literals are different.
Two strings can be compared using comparison operators.
The operators compare strings alphabetically.
The localeCompare() method compares strings according to the current language settings.
JavaScript strings have useful properties.
| Property | Description |
|---|---|
| length | Returns the number of characters in a string. |
| Method | Purpose |
|---|---|
| charAt() | Returns character at a position. |
| concat() | Joins two or more strings. |
| indexOf() | Finds first occurrence. |
| lastIndexOf() | Finds last occurrence. |
| replace() | Replaces text. |
| slice() | Extracts part of a string. |
| split() | Converts string to array. |
| substring() | Extracts characters. |
| toLowerCase() | Converts to lowercase. |
| toUpperCase() | Converts to uppercase. |
| Method | Description |
|---|---|
| match() | Searches using regular expressions. |
| search() | Finds matching text. |
| substr() | Extracts characters by length. |
| valueOf() | Returns primitive value. |
| toString() | Returns string representation. |
| localeCompare() | Compares two strings. |
| Concept | Example |
|---|---|
| Single Quote | 'Hello' |
| Double Quote | "Hello" |
| Template Literal | `Hello ${name}` |
| Length | str.length |
| Uppercase | str.toUpperCase() |
| Lowercase | str.toLowerCase() |
| Concatenation | str1 + str2 |
Which symbol is used for JavaScript template strings?