0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Decimal to Octal Number

Use number.toString(8) in JavaScript to convert a decimal number to its octal string representation, for example, (10).toString(8) returns '12'.
📋

Examples

Input8
Output'10'
Input64
Output'100'
Input0
Output'0'
🧠

How to Think About It

To convert decimal to octal, think of changing the base from 10 to 8. JavaScript lets you do this easily by converting the number to a string in base 8 using toString(8). This gives the octal form as a string.
📐

Algorithm

1
Get the decimal number input.
2
Use the number's <code>toString</code> method with 8 as the argument.
3
Return the resulting string as the octal representation.
💻

Code

javascript
const decimalNumber = 83;
const octalString = decimalNumber.toString(8);
console.log(octalString);
Output
123
🔍

Dry Run

Let's trace converting decimal 83 to octal using toString(8).

1

Start with decimal number

decimalNumber = 83

2

Convert to octal string

octalString = 83..toString(8) => '123'

3

Print result

console.log(octalString) outputs '123'

DecimalOctal String
83123
💡

Why This Works

Step 1: Using toString with base 8

The toString(8) method converts the decimal number into a string representing the number in base 8 (octal).

Step 2: Result is a string

The output is a string because octal numbers are usually represented as strings in programming.

Step 3: Works for any decimal number

This method works for any positive integer decimal number to convert it to octal.

🔄

Alternative Approaches

Manual division and remainder
javascript
function decimalToOctal(num) {
  let octal = '';
  while (num > 0) {
    octal = (num % 8) + octal;
    num = Math.floor(num / 8);
  }
  return octal || '0';
}
console.log(decimalToOctal(83));
This method shows the manual process of dividing by 8 and collecting remainders but is longer and less concise.
Using parseInt with radix
javascript
const decimalNumber = 83;
const octalString = parseInt(decimalNumber, 10).toString(8);
console.log(octalString);
This is similar to the main method but explicitly parses the number first; useful if input is a string.

Complexity: O(log n) time, O(log n) space

Time Complexity

Converting a number to a string in base 8 takes time proportional to the number of digits in octal, which is about log base 8 of the number.

Space Complexity

The space needed is proportional to the length of the octal string, also about log base 8 of the number.

Which Approach is Fastest?

Using toString(8) is the fastest and simplest built-in method compared to manual division.

ApproachTimeSpaceBest For
toString(8)O(log n)O(log n)Quick and simple conversion
Manual divisionO(log n)O(log n)Learning how base conversion works
parseInt + toStringO(log n)O(log n)When input is string and needs parsing
💡
Use number.toString(8) for a quick and easy decimal to octal conversion in JavaScript.
⚠️
Beginners often forget to use toString(8) and try to convert numbers directly without specifying the base.