string.padStart() เพิ่มสตริงที่ 2 ไปด้านหน้าสตริงแรก จนครบความยาวที่กำหนด รับพารามิเตอร์ 2 ตัว: ความยาวเป้าหมาย และสตริงที่ต้องการเติม
ไวยากรณ์
string.padStart(length, "string");
ตัวอย่าง
<html> <body> <script>
var str = "the best";
var st = "Hello";
document.write(st.padStart(24, " glad to meet you, "));
document.write("<br>");
document.write(str.padStart(16, "Tutorix "));
</script> </body> </html>
ผลลัพธ์
glad to meet you, Hello Tutorix the best
ถ้าสตริงต้นฉบับยาวกว่าความยาวที่กำหนด จะไม่มีการเปลี่ยนแปลง
ตัวอย่าง สตริงยาวกว่าความยาวเป้าหมาย
<html> <body> <script>
var str = "Tutorix is the best e-learning platform best";
document.write(str.padStart(16, "Tutorix "));
</script> </body> </html>
ผลลัพธ์
Tutorix is the best e-learning platform best