For example if I have the folowing javascript code:
let num = 00111;
console.log(num.toString());
This would give me '73'
Yet I would want '00111'
For example if I have the folowing javascript code:
let num = 00111;
console.log(num.toString());
This would give me '73'
Yet I would want '00111'
The issue with why the number is not-as-you-expect is because leading 0s in an integer literal tell Javascript the number is an octal literal, an integer with radix 8.
To get what you want, you can either remove the leading zeros from the literal and then add them back later:
let num = 111;
console.log('00' + num.toString());
Or you can store the value as a string, and parse it:
let value = '00111':
let num = parseInt(value); // assumes radix 10.
console.log('00' + num.toString());
If the goal is merely counting bits represented by 1s (which would explain the '3' in the example), you can write a helper function that would count the number of 1s in a string, and then use that to extract the value, as has been done here.
If the goal is a literal binary number (7, not 3), then you can use binary literals:
let num = 0b00111;
console.log(num); // 7
console.log(num.toString(2)); // 111
Number.prototype.toString()
has a radix parameter that converts your string to any number in that radix.
So, if you want to convert your number to a string value of its JavaScript octal notation, you can do this:
let num = 00111;
let str = `00${num.toString(8)}`;
console.log(str);