JavaScript String Manipulation Essentials
Accessing the length property returns the total number of characters in a string.
const greeting = "Hi";
console.log(greeting.length); // 2
const empty = "";
console.log(empty.length); // 0
Extracting Segments with substring
The substring(start, end) method extracts characters between two indices. The end index is exclusive. If start exceeds end, the arguments are swapped. Negative values are treated as zero.
const phrase = "coding";
console.log(phrase.substring(1)); // "oding"
console.log(phrase.substring(0, 3)); // "cod"
console.log(phrase.substring(4, 1)); // "odi" (swaps 1 and 4)
Accessing Characters with charAt
Retrieves the character at a specified zero-based position. Out-of-bounds indices return an empty string.
const word = "dev";
console.log(word.charAt(0)); // "d"
console.log(word.charAt(10)); // ""
Substituting Text with replace
Replaces the first occurrence of a matched substring or regular expression. To replace all occurrences with a regex, the global (g) flag is required.
const text = "2023-03-15";
console.log(text.replace("-", "/")); // "2023/03-15"
console.log(text.replace(/-/g, "/")); // "2023/03/15"
Global Substitution with replaceAll
Replaces every occurrence of a matching substring or regex. Using a regex requires the global flag.
const digits = "0A0B0";
console.log(digits.replaceAll("0", "X")); // "XAXBX"
Tokenizing Strings with split
Divides a string into an array of substrings based on a delimiter. Passing an empty string splits by each character.
const dateStr = "2023/03/15";
console.log(dateStr.split("/")); // ["2023", "03", "15"]
console.log(dateStr.split("")); // ["2", "0", "2", "3", "/", ...]
Extracting Segments with slice
Returns a portion of the string from beginIndex to endIndex (exclusive). Unlike substring, it handles negative indices, which count backward from the end.
const msg = "frontend";
console.log(msg.slice(0, 4)); // "fron"
console.log(msg.slice(-3)); // "end"
Merging Strings with concat
Joins multiple strings together, returning a new combined string. Non-string arguments are implicitly converted.
const a = "web";
const b = "dev";
console.log(a.concat("-", b)); // "web-dev"
Case Conversion
Transfomrs all alphabetic characters to either uppercase or lowercase, leaving numbers and symbols unaffected.
const mixed = "Js2023";
console.log(mixed.toUpperCase()); // "JS2023"
console.log(mixed.toLowerCase()); // "js2023"
Removing Whitespace with trim
Strips whitespace (spaces, tabs, newlines) from both ends of a string. Internal whitespace remains intact.
const padded = " api call ";
console.log(padded.trim()); // "api call"
Replicating Strings with repeat
Constructs a new string by repeating the original string a spceified number of times.
const dash = "-";
console.log(dash.repeat(4)); // "----"
Prefix and Suffix Validation
Checks whether a string begins or ends with a specific sequence of characters, returning a boolean.
const url = "https://api.io";
console.log(url.startsWith("https")); // true
console.log(url.endsWith(".io")); // true
Finding First Occurrence with indexOf
Returns the starting position of the first instance of a substring. If the substring is absent, it returns -1. A second argument specifies the starting search position.
const path = "/user/profile";
console.log(path.indexOf("/")); // 0
console.log(path.indexOf("/", 1)); // 5
Finding Last Occurrence with lastIndexOf
Searches backward and returns the position of the final occurrence of a specified substring, or -1 if not found.
const log = "error: fatal error";
console.log(log.lastIndexOf("error")); // 13