「JavaScript」padStartメソッドで頭0をつけるサンプル

2021年5月24日

書式
文字列.padStart(文字列の長さ[,埋める文字]);
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script>
const cftA = "8";
console.log(cftA.padStart(3, "0"));
console.log(cftA.padStart(4, "0"));
const cftB = "60";
console.log(cftB.padStart(3, "0"));
console.log(cftB.padStart(4, "0"));
</script>
<script> const cftA = "8"; console.log(cftA.padStart(3, "0")); console.log(cftA.padStart(4, "0")); const cftB = "60"; console.log(cftB.padStart(3, "0")); console.log(cftB.padStart(4, "0")); </script>
<script>
    const cftA = "8";
    console.log(cftA.padStart(3, "0")); 
    console.log(cftA.padStart(4, "0"));

    const cftB = "60";
    console.log(cftB.padStart(3, "0")); 
    console.log(cftB.padStart(4, "0")); 
</script>

実行結果
008
0008
060
0060

JavaScript

Posted by arkgame