Search code examples
javascriptpythonmathadditionradix

why does (00123 + 34351) return 34434 in javascript, java and python ? The correct answer is 34474


console.log(00123 + 34351); //in javascript, outputs 34434
print(00123 + 34351); //in python, outputs 34434

in javascript, outputs 34434 in python, outputs 34434

However removing the leading zeroes i.e (123 + 34351) gives the correct answer, which is 34474


Solution

  • 0 is a common prefix for octal, for which the decimal number is 83. Doing

    console.log(00123+34351)
    

    is equivalent to

    console.log(83+34351)
    

    edit: note that in python 3+ the prefix is 0o