A string is a sequence of characters used to represent text. Strings are one of the most commonly used data types in JavaScript and are enclosed in quotes, either single ('
), double ("
) or backticks (`
) for template literals.
Creating Strings
You can create strings using different types of quotes:
let singleQuote = 'Hello, World!'; let doubleQuote = "Hello, World!"; let templateLiteral = `Hello, World!`; console.log(singleQuote); // Output: Hello, World! console.log(doubleQuote); // Output: Hello, World! console.log(templateLiteral); // Output: Hello, World!
Key Points:
- Single and double quotes work similarly.
- Template literals (backticks) allow for multi-line strings and string interpolation.
String Length
The length
property returns the number of characters in a string.
let text = 'JavaScript'; console.log(text.length); // Output: 10
String Methods
JavaScript provides many methods to work with strings.
1. charAt()
Returns the character at a specified index.
let text = 'Hello'; console.log(text.charAt(0)); // Output: H
2. concat()
Joins two or more strings.
let text1 = 'Hello'; let text2 = 'World'; console.log(text1.concat(' ', text2)); // Output: Hello World
3. includes()
Checks if a string contains a specified value.
let text = 'Hello, World!'; console.log(text.includes('World')); // Output: true
4. indexOf()
Returns the index of the first occurrence of a specified value.
let text = 'Hello, World!'; console.log(text.indexOf('World')); // Output: 7
5. slice()
Extracts a section of a string and returns it as a new string.
let text = 'Hello, World!'; console.log(text.slice(7, 12)); // Output: World
6. toLowerCase()
/ toUpperCase()
Converts a string to lowercase or uppercase.
let text = 'JavaScript'; console.log(text.toLowerCase()); // Output: javascript console.log(text.toUpperCase()); // Output: JAVASCRIPT
7. trim()
Removes whitespace from both ends of a string.
let text = ' Hello, World! '; console.log(text.trim()); // Output: 'Hello, World!'
Template Literals
Template literals allow for string interpolation and multi-line strings.
let name = 'John'; let greeting = `Hello, ${name}! Welcome to JavaScript.`; console.log(greeting); // Output: Hello, John! Welcome to JavaScript.
Key Points:
- Enclosed in backticks (
`
). - Can span multiple lines.
- Support expression interpolation using
${expression}
.
Escape Characters
Special characters in strings can be escaped using a backslash (\
).
let text = 'He said, "It\'s a beautiful day!"'; console.log(text); // Output: He said, "It's a beautiful day!"
Common escape sequences:
\'
– Single quote\"
– Double quote\\
– Backslash\n
– New line\t
– Tab
Summary
- Strings are sequences of characters used to represent text.
- Can be created using single, double, or backticks.
- Include various methods like
charAt()
,concat()
,slice()
,toLowerCase()
,toUpperCase()
, and more. - Template literals provide an easier way to include variables and expressions in strings.
- Use escape characters to include special characters in strings.