In JavaScript, a String is a primitive data type used to represent a sequence of characters. Strings are immutable, meaning once a string is created, its value cannot be changed; instead, any operation that appears to modify a string actually returns a brand-new string.

Strings are stored as sequences of 16-bit unsigned integer values, typically encoded in UTF-16. This allows JavaScript to represent a wide range of global characters and emojis.

1. Creating Strings

JavaScript offers three distinct ways to define string literals, each with unique capabilities.

A. Single and Double Quotes

Standard strings are wrapped in ' ' or " ". There is no functional difference between them, but they cannot span multiple lines without escape characters.

JavaScript
const single = 'Hello';
const double = "World";

B. Template Literals (Backticks)

Introduced in ES6, backticks (`) are the modern standard. They support:

  • Multi-line strings: No need for \n.
  • Interpolation: Embedding variables or expressions via ${expression}.
JavaScript
const name = "Alice";
const greeting = `Hello, ${name}!
Welcome to the dashboard.`;

2. Character Access and Immutability

You can access individual characters using bracket notation or the charAt() method. However, because strings are immutable, you cannot reassign a character at a specific index.

JavaScript
let str = "Hello";
console.log(str[0]); // "H"

str[0] = "J";        // Fails silently (or throws an error in strict mode)
console.log(str);    // Still "Hello"

3. String Properties and Indexing

Every string has a length property that returns the number of code units in the string. You can access individual characters using bracket notation or the .charAt() method.

JavaScript
const str = "JavaScript";
console.log(str.length); // 10
console.log(str[0]);     // "J"
console.log(str.charAt(4)); // "S"

Note: Because strings are immutable, str[0] = "Z" will fail silently (or throw an error in strict mode); the string remains “JavaScript”.

4. The String Object vs. Primitive

When you call a method on a primitive string, JavaScript temporarily wraps it in a String Object, executes the method, and then discards the object. This is known as “autoboxing.” You can manually create a String object using new String(), but this is generally discouraged as it can lead to unexpected results during type comparison.

JavaScript
const prim = "text";
const obj = new String("text");

console.log(typeof prim); // "string"
console.log(typeof obj);  // "object"
console.log(prim == obj); // true (value check)
console.log(prim === obj);// false (type check)

5. String Concatenation

In modern engines (V8, SpiderMonkey), the + operator is highly optimized for joining strings. However, for building extremely large strings in loops, joining an array is often considered a traditional alternative:

JavaScript
// Usually fine
let str = "a" + "b" + "c";

// Often faster for massive iterations
let parts = [];
for(let i=0; i<1000; i++) parts.push(i);
let final = parts.join("");

6. String to Array Conversions

Strings and Arrays share many similarities (both are indexable and have a length), which makes conversion between them a common task.

  • split(separator): Breaks a string into an array.
  • Array.from(string): Converts a string into an array of characters (correctly handles surrogate pairs/emojis).
  • Spread Operator […string]: Similar to Array.from.

JavaScript

const word = "Hello";
const chars = [...word]; // ["H", "e", "l", "l", "o"]

Categorized in:

Javascript,