Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
A JavaScript String is a primitive data type representing an immutable sequence of zero or more 16-bit unsigned integer values. Each element in the string occupies a specific index and represents a UTF-16 code unit.
Instantiation and Syntax
Strings can be created as primitives using literal syntax or as objects using the String constructor.
// String Literals (Primitives)
const singleQuotes = 'literal';
const doubleQuotes = "literal";
const templateLiteral = `literal`; // Supports expression interpolation: `${var}`
// Type Coercion (Primitive)
const coercedString = String(100n); // "100"
// String Object (Wrapper Object)
const stringObject = new String("object");
typeof singleQuotes; // "string"
typeof stringObject; // "object"
Immutability
String primitives are strictly immutable. Once a string is created, its sequence of code units cannot be altered. Operations that appear to modify a string actually allocate a new string in memory and return the new reference.
let str = "Byte";
str[0] = "b"; // Fails silently (or throws TypeError in strict mode)
console.log(str); // "Byte"
str = "byte"; // Reassignment of the variable, not modification of the original string
Encoding and the length Property
JavaScript strings use UTF-16 encoding. The length property returns the number of 16-bit code units, not necessarily the number of visible characters (graphemes). Characters within the Basic Multilingual Plane (BMP) require one code unit, while astral plane characters (like emojis or rare symbols) require two code units, known as a surrogate pair.
const bmpChar = "A";
console.log(bmpChar.length); // 1
const astralChar = "🚀";
console.log(astralChar.length); // 2 (High and low surrogate code units)
Autoboxing
When a property or method is accessed on a string primitive, JavaScript temporarily coerces the primitive into a String wrapper object, executes the method, and immediately discards the wrapper. This allows primitives to utilize the String.prototype API without the memory overhead of permanent objects.
const primitive = "data";
// JS engine internally executes: new String("data").toUpperCase()
const upper = primitive.toUpperCase();
Character Access
Individual code units can be accessed via zero-based indexing.
const str = "Node";
// Bracket notation (returns undefined if out of bounds)
str[0]; // "N"
// charAt() method (returns empty string if out of bounds)
str.charAt(1); // "o"
// at() method (supports negative integers for reverse indexing)
str.at(-1); // "e"
Core String.prototype API
The prototype provides methods for inspection, extraction, and transformation. All transformation methods return a new string.
Extraction:
const str = "JavaScript";
str.slice(0, 4); // "Java" (Extracts from start index to end index)
str.substring(4, 10); // "Script" (Similar to slice, but swaps indices if start > end)
Inspection and Searching:
const str = "Runtime";
str.indexOf("time"); // 3 (Returns starting index, or -1 if not found)
str.includes("Run"); // true (Returns boolean)
str.startsWith("Run"); // true
str.endsWith("me"); // true
str.match(/[a-z]+/g); // ["untime"] (Executes regex search, returns array of matches)
Transformation:
const str = " Parse ";
str.trim(); // "Parse" (Removes leading/trailing whitespace)
str.replace("P", "p"); // " parse " (Replaces first match)
str.replaceAll(/\s/g, ""); // "Parse" (Replaces all matches)
str.toLowerCase(); // " parse "
str.toUpperCase(); // " PARSE "
str.concat(" Tree"); // " Parse Tree"
Deconstruction:
const str = "a,b,c";
str.split(","); // ["a", "b", "c"] (Splits string into an array based on a delimiter)
Master JavaScript with Deep Grasping Methodology!Learn More