JavaScript: String.prototype.padStart

By Xah Lee. Date: . Last updated: .

New in JS2017.

str.padStart(n)
Add spaces in front so the length is n. Return the new string.
str.padStart(n, padstr)
Use padStr to pad.
console.log( "x".padStart(3) === "  x");
console.log( "x".padStart(3, "0") === "00x");
console.log( "x".padStart(4, "ab") === "abax");
// all true
console.log( "😂".padStart(2,"x") === "😂" );
// no padding happens, because "😂" is already length 2

// 😂
// name: FACE WITH TEARS OF JOY
// codepoint decimal: 128514
// codepoint hexadecimal: 1f602
// UTF8 encoding: F0 9F 98 82
// Utf16 encoding: D8 3D DE 02

Note: "😂".length === 2 [see Character, Code Unit, Codepoint]

BUY
ΣJS
JavaScript in Depth

JS Obj Reference

String

prototype