String Basics0%

String Basics

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

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:

JavaScript
// 1. Single Quotes
const city = 'New Delhi';
 
// 2. Double Quotes
const country = "India";
 
// 3. Backticks (Template Literals - ES6)
const institute = `MSK Institute`;

Escaping Special Characters (\):

If a string contains quotation marks inside it, escape them with a backslash \:

JavaScript
const quote = "She said, \"JavaScript is amazing!\"";
const contraction = 'It\'s a sunny day';

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:

Output
String: "M S K"
Index: 0 1 2
JavaScript
const school = "MSK Institute";
 
// Check total character count:
console.log(school.length); // 13 (Includes the space!)
 
// Accessing individual characters:
console.log(school[0]); // "M" (Bracket notation)
console.log(school.charAt(1)); // "S" (charAt method)
 
// Getting the last character:
console.log(school[school.length - 1]); // "e"
 
// Modern ES2022 .at() method (supports negative index!):
console.log(school.at(-1)); // "e" (Last character)
console.log(school.at(-2)); // "t" (Second to last)

3. The Golden Rule: Strings are Immutable!

[!IMPORTANT] Strings in JavaScript are 100% IMMUTABLE. Once created in memory, individual characters inside a string cannot be changed in place!
JavaScript
let str = "Hello";
str[0] = "J"; // Trying to change "H" to "J"
 
console.log(str); // "Hello"! (Unchanged! JavaScript silently ignores this assignment)

How to "Change" a String:

To change a string, you must create and assign a brand new string:

JavaScript
let str = "Hello";
str = "J" + str.slice(1);
console.log(str); // "Jello"

4. Iterating Through Strings

Because strings are iterable, you can loop through their characters using a for loop or for...of:

JavaScript
const word = "CODE";
 
for (const char of word) {
console.log(char);
}
// Outputs:
// C
// O
// D
// E

Interactive Quiz

Practice Quiz

Question 1
What index number represents the very first character of a string in JavaScript?
Question 2
What is the result of running let s = "Hello"; s[0] = "Y"; console.log(s);?
Question 3
How do you get the total number of characters in a string named msg?
Question 4
Which modern method introduced in ES2022 allows accessing the last character of a string using negative indexing (e.g. str.at(-1))?
Question 5
What escape sequence inserts a line break (new line) inside a string literal?
Next Lesson

Common String Methods

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext