Highlighted

Why this toString() method acting weird?

Contributor ,
May 14, 2020

Copy link to clipboard

Copied

I know this is very basic. But when I use this code. I expect the output to be 043 but it alerts 35 instead. What I'm missing here?

 

var num = 043;
alert(num.toString());

 

TOPICS
Error or problem, Resources, Scripting

Views

700

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Why this toString() method acting weird?

Contributor ,
May 14, 2020

Copy link to clipboard

Copied

I know this is very basic. But when I use this code. I expect the output to be 043 but it alerts 35 instead. What I'm missing here?

 

var num = 043;
alert(num.toString());

 

TOPICS
Error or problem, Resources, Scripting

Views

701

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 14, 2020 0
Adobe Community Professional ,
May 14, 2020

Copy link to clipboard

Copied

Numbers are just numbers and don't have any formatting attached to them.

No matter if you write 043 or 43 or 40+3 it all evaluates to the same JavaScript value.

If you want to save a specific format, you need to store it as a string

var num = "043";
alert(num);

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 14, 2020 1
Contributor ,
May 14, 2020

Copy link to clipboard

Copied

Actually I want to convert an array of numbers to string. It doesn't help me to convert by this way or it doesn't let me to convert some numbers to string. I've even tried .join.split(',') method to convert it. Still it don't work. Can you help me to overcome this issue?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 14, 2020 0
Explorer ,
May 14, 2020

Copy link to clipboard

Copied

I was curious about your problem and i ran a few searches.

 

In javascript, numbers starting with 0 are octa and starting with 0x are hexa. So running num.toString(8) returns the correct number, but cuts the 0.

So your number is not 043 decimal. If, for example, you add 043+5, the result is 40.

You said you need to convert an array of numbers to string. Why do you have a 043 number? If you need it to be number, should it not be 43?

As Mathias said, you should store it as a string from beggining if you want it in that format.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 14, 2020 1