Back to Library
Region:
Switch to ID
Beginner freeCodeCamp • introduction-to-strings
What Is a String in JavaScript, and What Is String Immutability?
Lesson Overview
In JavaScript, a **String** is a sequence of characters used to represent text. It is one of the most commonly used data types in web development.
In JavaScript, a String is a sequence of characters used to represent text. It is one of the most commonly used data types in web development.
Creating Strings
You can create strings using three different types of quotes:
- Single Quotes:
'Hello' - Double Quotes:
"Hello" - Backticks (Template Literals):
`Hello`
String Immutability
A very important concept in JavaScript is that strings are immutable. This means that once a string is created, its individual characters cannot be changed.
let myString = "Bob";
myString[0] = "J"; // ❌ This will NOT change the string to "Job"
console.log(myString); // Output: "Bob"
If you want to “change” a string, you must create a new one:
myString = "Job"; // ✅ This assigns a completely new string to the variable
String Length
You can find out how many characters are in a string using the .length property:
let word = "Astro";
console.log(word.length); // Output: 5