Created
April 28, 2022 06:14
-
-
Save tatsuyasusukida/935c7d58609240e96bd8960a81b555b7 to your computer and use it in GitHub Desktop.
JavaScript padStart alternative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (require.main === module) { | |
main() | |
} | |
async function main () { | |
try { | |
const value = 1234 | |
const str = value.toString(16) | |
const expected = '0x' + str.padStart(4, '0') | |
const step1 = '000' + str // <1> | |
const step2 = step1.slice(-4) // <2> | |
const step3 = '0x' + step2 // <3> | |
const actual = step3 | |
// const actual = '0x' + ('000' + str).slice(-4) | |
console.log({expected, actual}) | |
} catch (err) { | |
console.error(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment