--- id: javascript-string-methods title: "JavaScript String Methods" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["string methods", "slice", "substring", "trim", "replace", "split", "padStart"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "strings", "string-methods", "immutability"] raw_sources: ["https://www.w3schools.com/js/js_string_methods.asp"] applied_in: [] github_commit: "" --- # [[JavaScript String Methods]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Strings are immutable, so every string method produces a new string without altering the original β€” covering extraction (`slice`, `substring`, `substr`), character access (`charAt`, `at`, `[]`), case, trimming, padding, and replacement. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Immutability** β€” all string methods produce a new string without altering the original string; strings cannot be changed, only replaced. [S1] - **`length`** β€” the `length` property returns the length of a string. [S1] - **Character extraction** β€” four ways to get a character: `at()`, `charAt()`, `charCodeAt()`, and bracket notation `[]`; `codePointAt()` returns a code point. [S1] - **`at()` allows negative indexes** β€” introduced in ES2022, `at()` allows negative indexes while `charAt()` does not. [S1] - **Substring extraction** β€” `slice()`, `substring()`, and `substr()` extract parts of a string; `substr()` is deprecated. [S1] - **`replace()` vs `replaceAll()`** β€” `replace()` replaces only the first match and is case sensitive by default; `replaceAll()` (ES2021) replaces all matches. [S1] - **`split()`** β€” converts a string into an array using a chosen delimiter. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Extract a slice by index range** β€” `text.slice(start, end)` (or `substring`) to pull a labeled portion. [S1] - **Normalize input** β€” chain `trim()` / `trimStart()` / `trimEnd()` then case methods to clean user input. [S1] - **Pad to a fixed width** β€” `padStart(len, "0")` to left-pad values such as IDs or amounts. [S1] - **Replace all occurrences** β€” `replaceAll(a, b)`, or `replace()` with a `/g` regex flag, since plain `replace()` only hits the first match. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) > Note: All string methods produce a new string without altering the original string. Strings are immutable: strings cannot be changed, only replaced. [S1] **Length** [S1] ```javascript let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; let length = text.length; ``` **charAt()** β€” returns the character at a given index. [S1] ```javascript let text = "HELLO WORLD"; let char = text.charAt(0); ``` **charCodeAt()** β€” returns the UTF-16 code of the character at a given index. [S1] ```javascript let text = "HELLO WORLD"; let char = text.charCodeAt(0); ``` **codePointAt()** β€” returns the Unicode code point at a given index. [S1] ```javascript let text = "HELLO WORLD"; let code = text.codePointAt(0); ``` **at()** β€” introduced in ES2022; allows negative indexes while `charAt()` does not. [S1] ```javascript const name = "W3Schools"; let letter = name.at(2); ``` **Property access []** β€” bracket notation reads a character by index. [S1] ```javascript let text = "HELLO WORLD"; let char = text[0]; ``` **concat()** β€” joins two or more strings. [S1] ```javascript let text1 = "Hello"; let text2 = "World"; let text3 = text1.concat(" ", text2); ``` **slice()** β€” extracts a part of a string between start and end positions. [S1] ```javascript let text = "Apple, Banana, Kiwi"; let part = text.slice(7, 13); ``` **substring()** β€” similar to `slice()`. [S1] ```javascript let str = "Apple, Banana, Kiwi"; let part = str.substring(7, 13); ``` **substr()** β€” deprecated; second parameter is the length to extract. [S1] ```javascript let str = "Apple, Banana, Kiwi"; let part = str.substr(7, 6); ``` **toUpperCase()** [S1] ```javascript let text1 = "Hello World!"; let text2 = text1.toUpperCase(); ``` **toLowerCase()** [S1] ```javascript let text1 = "Hello World!"; let text2 = text1.toLowerCase(); ``` **trim()** β€” removes whitespace from both sides. [S1] ```javascript let text1 = " Hello World! "; let text2 = text1.trim(); ``` **trimStart()** β€” removes whitespace from the start. [S1] ```javascript let text1 = " Hello World! "; let text2 = text1.trimStart(); ``` **trimEnd()** β€” removes whitespace from the end. [S1] ```javascript let text1 = " Hello World! "; let text2 = text1.trimEnd(); ``` **padStart()** β€” pads from the start to a given length (ES2017). [S1] ```javascript let text = "5"; let padded = text.padStart(4,"0"); ``` **padEnd()** β€” pads from the end to a given length (ES2017). [S1] ```javascript let text = "5"; let padded = text.padEnd(4,"0"); ``` **repeat()** β€” returns a new string with a number of copies. [S1] ```javascript let text = "Hello world!"; let result = text.repeat(2); ``` **replace()** β€” the `replace()` method replaces only the first match. By default, the `replace()` method is case sensitive. [S1] ```javascript let text = "Please visit Microsoft!"; let newText = text.replace("Microsoft", "W3Schools"); ``` **replaceAll()** β€” replaces all matches (ES2021); use a `/g` flag to replace all matches with regular expressions. [S1] ```javascript text = text.replaceAll("Cats","Dogs"); text = text.replaceAll("cats","dogs"); ``` **split()** β€” converts a string into an array using a delimiter. [S1] ```javascript text.split(",") // Split on commas text.split(" ") // Split on spaces text.split("|") // Split on pipe ``` Other methods listed by the page include `isWellFormed()` and `toWellFormed()`. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” extracting substrings from `"Apple, Banana, Kiwi"`, padding `"5"` to width 4, replacing `"Microsoft"` with `"W3Schools"`, and splitting on delimiters. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Extract a portion: ```javascript let part = text.slice(7, 13); ``` Pad to fixed width: ```javascript let padded = text.padStart(4,"0"); ``` Replace every occurrence: ```javascript text = text.replaceAll("Cats","Dogs"); ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) `substr()` is noted as deprecated; prefer `slice()` or `substring()`. No other contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Strings]], [[JavaScript String Search]], [[JavaScript String Templates]], [[JavaScript Arrays]] - **μ°Έμ‘° λ§₯락:** Referenced whenever transforming, slicing, cleaning, or replacing text values. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript String Methods β€” https://www.w3schools.com/js/js_string_methods.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript String Methods" page (Astra wiki-curation, P-Reinforce v3.1 format).