Search code examples
javascriptnode.jsnumbersoctal

javascript remove leading zeros from a number


const a = 043      
console.log(Number(a))       

Here the variable a is octal because of which we get the result as 35. Instead, I want the variable a to be a number 43. Found results for removing leading zeros from a string(Remove leading zeros from a number in Javascript)


Solution

  • Because your value starts with 0, and its type is number instead of string, it will be recognized as octal, and when you use it directly, javascript will convert it to decimal

    The fastest way you can use Number.prototype.toString(radix)

    let a = 043
    console.log(a.toString(8))