String Object
The String object is a wrapper for string primitives. It provides methods for manipulating and inspecting text. Strings are immutable; methods return new strings.
Key Points
Section titled “Key Points”- Properties:
length. - Methods:
charAt(),indexOf(),slice(),substring(),split(),replace(),toUpperCase(),trim(), etc. - Template literals (backticks) allow interpolation and multi‑line strings.
Code Example
Section titled “Code Example”const str = "Hello, World!";console.log(str.length); // 13console.log(str.charAt(0)); // "H"console.log(str.indexOf("World")); // 7console.log(str.slice(7, 12)); // "World"console.log(str.toUpperCase()); // "HELLO, WORLD!"console.log(str.replace("World", "JavaScript")); // "Hello, JavaScript!"
const name = "Alice";const greeting = `Hello, ${name}!`; // template literalconsole.log(greeting); // "Hello, Alice!"