For the common case, use str.slice(0, -1). It returns a new string with the final UTF-16 code unit removed; it does not change the original string. If by “character” you mean a whole visible symbol, choose a Unicode-aware approach instead.
Remove the final UTF-16 code unit
TypeScript uses JavaScript string values, so JavaScript’s slice method works directly:
const shortened = str.slice(0, -1);
The negative end index counts back from the string’s end. The result includes everything before the final code unit and excludes that last unit. Since strings are immutable, assign or return the result rather than expecting str itself to change. If str is empty, the result is also an empty string. TypeScript’s basic types guidance describes strings as primitive values.
Choose the unit you actually want to remove
In everyday language, “character” can mean a UTF-16 code unit, a Unicode code point, or a user-perceived character. Those are not always the same thing.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →#1 Best Overall
| What to remove | Approach | Important distinction |
|---|---|---|
| Final UTF-16 code unit | str.slice(0, -1) |
Concise; can remove only half of a surrogate pair. |
| Final Unicode code point | Array.from(str).slice(0, -1).join("") |
Handles surrogate pairs as one code point, but treats combining marks separately from their base characters. |
| Final user-perceived character, or grapheme cluster | Use grapheme segmentation, such as Intl.Segmenter where supported by the target runtime. |
Suitable for sequences such as a base letter plus a combining mark or multi-code-point emoji; check support in the runtime you target. |
Why a visible symbol can be split
JavaScript string indexing and length are based on UTF-16 code units. Some symbols, including supplementary-plane characters such as 𠮷, are encoded as a pair of code units. Removing one final code unit from a string ending in that symbol can leave half of the surrogate pair, not remove the complete symbol. The TypeScript 1.5 release notes explain this encoding for 𠮷.
Even code-point-aware removal may not match what a person sees as one character: a letter and its combining mark are separate code points, and some emoji are sequences of multiple code points. For text-editing behavior, decide whether your requirement is about code points or grapheme clusters, then test that behavior against the text your application handles.
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
Make the function’s behavior clear
If the function removes a code unit, name it accordingly:
function removeLastCodeUnit(value: string): string {
return value.slice(0, -1);
}
Use the primitive TypeScript type string, not the boxed String type; the TypeScript handbook’s basic types guidance recommends primitive types.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




