String Basics

Strings in JavaScript are sequences of characters used to represent text. They are one of the most common data types and can be created using single quotes ('...' ), double quotes ("..."), or backticks (`...`).

Illustration of string_basics
Illustration of string_basics

💡 Creating Strings

All of these lines define the same string:

  • let single = 'Hello';
  • let double = "Hello";
  • let backtick = `Hello`; (allows embedding expressions)

Strings are immutable, meaning once created, their characters cannot be changed directly.

⚠️ Strings are Immutable

You cannot change a character inside a string by assigning to an index. Instead, create a new string if you want to modify it.

Common String Characteristics
FeatureDescription
ImmutableStrings cannot be changed after creation
IndexedCharacters can be accessed by zero-based index (e.g., str[0])
LengthStrings have a length property telling how many characters they contain
MethodsStrings come with many useful methods for manipulation and inspection

Accessing characters and length example:

📌 Deep Dive: Access & Length

JAVASCRIPT
const greeting = "Hello";
console.log(greeting[0]);       // H
console.log(greeting.length);   // 5
console.log(greeting[greeting.length - 1]); // o
Output
H
5
o

Strings can include special characters using escape sequences:

  • - newline
  • \t - tab
  • \\ - backslash
  • \" or \' - quotes inside strings

📌 Deep Dive: Escape Sequences

JAVASCRIPT
const multiLine = "Line 1
Line 2";
const quote = "She said, \"Hello!\"";
console.log(multiLine);
console.log(quote);
Output
Line 1
Line 2
She said, "Hello!"

Template literals (backtick strings) allow embedding expressions and multiline strings easily:

📌 Deep Dive: Template Literals

JAVASCRIPT
const name = "Alice";
const age = 25;
const message = `My name is ${name} and I am ${age} years old.`;
console.log(message);
Output
My name is Alice and I am 25 years old.

💡 String Concatenation

Use the + operator to combine strings:

'Hello, ' + 'world!' results in 'Hello, world!'

Common useful string methods:

  • str.toUpperCase() - returns uppercase version
  • str.toLowerCase() - returns lowercase version
  • str.indexOf(substring) - finds position of substring or -1 if not found
  • str.includes(substring) - returns true if substring found
  • str.slice(start, end) - extracts a part of the string
  • str.trim() - removes whitespace from both ends

📌 Deep Dive: String Methods

JAVASCRIPT
const phrase = "  JavaScript is fun!  ";
console.log(phrase.trim());                 // "JavaScript is fun!"
console.log(phrase.toUpperCase());          // "  JAVASCRIPT IS FUN!  "
console.log(phrase.indexOf("is"));          // 12
console.log(phrase.includes("script"));     // false (case-sensitive)
console.log(phrase.slice(2, 12));            // "JavaScript"
Output
"JavaScript is fun!"
" JAVASCRIPT IS FUN! "
12
false
"JavaScript"

⚠️ Case Sensitivity

String methods like indexOf and includes are case-sensitive. Use toLowerCase() or toUpperCase() to normalize case if needed.