Lesson 14
Strings Basics
Strings are used to represent text. A string is a sequence of characters wrapped in single, double, or backticks.
- What is a String?
- Creating Strings
- Accessing Characters
- String Length
- Common Operations
- String Methods
- Template Literals
let message = "Hello, World!"| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Char | H | e | l | l | o | , | ␣ | W | o | r | l | d | ! |
Length: 13
A
Sequence of Characters
A string is a collection of characters.
❝
Can Use Quotes
Use single ( ' ), double ( " ) or backticks ( ` ).
#
Zero-based Indexing
First character is at index 0.
Has a Length
Use .length to find the number of characters.
JavaScript
Output
Your output will appear here...
| Operation | Example | Result | Description |
|---|---|---|---|
| Concatenation | 'Hello' + ' ' + 'DSA' | Hello DSA | Combines two or more strings. |
| toUpperCase() | 'hello'.toUpperCase() | HELLO | Converts string to uppercase. |
| toLowerCase() | 'HELLO'.toLowerCase() | hello | Converts string to lowercase. |
| includes() | 'Master DSA'.includes('DSA') | true | Checks if substring exists. |
| slice() | 'Master DSA'.slice(7, 10) | DSA | Extracts part of a string. |
| replace() | 'I love JS'.replace('JS', 'DSA') | I love DSA | Replaces substring. |
- // Length of string
str.length - // Character at index
str[index] - // Character at index
str.charAt(index) - // Substring
str.slice(start, end) - // Uppercase string
str.toUpperCase() - // Lowercase string
str.toLowerCase() - // Check substring
str.includes(sub)
Strings are immutable in JavaScript. Methods like toUpperCase() return a new string instead of changing the original.
