Skip to content
the poor coder

js octal

Poor Man
Published
Reading time
1 min read
On this page

What is Octal in JavaScript?#

Octal is a base-8 numbering system that uses the digits 0 through 7. In JavaScript, octal numbers are represented by a leading zero followed by one or more octal digits.

How to write Octal Numbers in JavaScript?#

You can write octal numbers in JavaScript by adding a leading zero before the digits. For example, the octal number 12 is written as 014 in JavaScript.

Text

    var octalNum = 014;
    console.log(octalNum); // Output: 12
  

Note that octal numbers are not commonly used in JavaScript and can lead to confusion in code. It is recommended to use decimal or hexadecimal numbers instead.

Converting Octal Numbers to Decimal#

To convert an octal number to decimal in JavaScript, you can simply use the Number() function.

Text

    var octalNum = 014;
    var decimalNum = Number(octalNum);
    console.log(decimalNum); // Output: 12
  

Converting Decimal Numbers to Octal#

To convert a decimal number to octal in JavaScript, you can use the toString() method with a radix of 8.

Text

    var decimalNum = 10;
    var octalNum = decimalNum.toString(8);
    console.log(octalNum); // Output: 12
  

Another way to convert a decimal number to octal is by using the parseInt() method with a radix of 8.

Text

    var decimalNum = 10;
    var octalNum = parseInt(decimalNum, 8);
    console.log(octalNum); // Output: 12
  

It is important to note that the leading zero is not necessary when converting from decimal to octal.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC