1

I am getting hands-on Node.js, and I found it pretty exciting. However, when I was trying to work out with variables I got strange results. When 01234 is assigned to a variable, 668 is displayed as response and when 0123 is assigned, 83 is returned as the response. I failed to understand logic. I am attaching this screengrab.

enter image description here

I checked same in c# and I got expected result. What is happening here in node.js?

2 Answers 2

2

In javascript, starting with 0 is indicating this number is in octal

012 = 2 + 1 * 8 = 10

Referenced here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Octal_interpretations_with_no_radix

Sign up to request clarification or add additional context in comments.

Comments

2

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

So 012 is an An octal numeral so that's why it is equal to 10 in the decimal.

A good solution is to parse it.

parseInt("012");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.