String Basics
String Basics in JavaScript
Text is one of the most fundamental data formats in software. In JavaScript, strings represent sequences of UTF-16 character codes. Understanding string indexing, length, and immutability is essential for building web interfaces.
1. Creating Strings in JavaScript
JavaScript supports three different quotation delimiters for string literals:
Escaping Special Characters (\):
If a string contains quotation marks inside it, escape them with a backslash \:
Useful Escape Sequences:
\n: Newline (line break)\t: Tab indentation\\: Literal backslash
2. String Length & Zero-Based Indexing
Strings behave similarly to arrays: characters are indexed starting at 0:
3. The Golden Rule: Strings are Immutable!
How to "Change" a String:
To change a string, you must create and assign a brand new string:
4. Iterating Through Strings
Because strings are iterable, you can loop through their characters using a for loop or for...of:
Practice Quiz
let s = "Hello"; s[0] = "Y"; console.log(s);?msg?str.at(-1))?Common String Methods
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Loop Control: break, continue | Common String Methods |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.