How to convert decimal to octal in JavaScript ? Last Updated : 12 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are given a number and the task is to convert the number from decimal to octal. This can be done by using number.toString(8) method. It takes the parameter which is the base of the converted string. In this case, the base will be 8. Syntax: number.toString(8) The below examples show the conversion from decimal to octal. Example 1: In this example, we will convert 34 and 24680 into octal numbers. javascript <script> var a = 34; var b = 24680; console.log("a = " + a.toString(8)); console.log("b = " + b.toString(8)); </script> Output: a = 42 b = 60150 Example 2: In this example, we will convert some numbers into octal numbers. javascript <script> dec_to_bho = function (n, base) { if (n < 0) { n = 0xFFFFFFFF + n + 1; } switch (base) { case 'O': return parseInt(n, 10).toString(8); break; case 'P': return parseInt(n, 10).toString(8); break; case 'Q': return parseInt(n, 10).toString(8); break; default: return ("Wrong input"); } } console.log(dec_to_bho(20, 'O')); console.log(dec_to_bho(56789, 'P')); console.log(dec_to_bho(321, 'Q')); </script> Output: 24 156725 501 Example 3: In this example, we will apply onclick function to convert some numbers into octal numbers. javascript <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <h3> Convert to octal </h3> <p id="up"></p> <button onclick="myGFG()"> Convert to octal </button> <p id="down" style="color: green"></p> <script> var GFG_Var = 134; var up = document.getElementById("up"); up.innerHTML = GFG_Var; var down = document.getElementById("down"); function myGFG() { var GFG_Var2 = GFG_Var.toString(8); down = document.getElementById("down"); down.innerHTML = "oct of " + GFG_Var + " is = " + GFG_Var2; } </script> </body> Output: Convert decimal to octal Comment More infoAdvertise with us Next Article How to Validate Decimal Numbers in JavaScript ? S shivanisinghss2110 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to convert decimal to hex in JavaScript ? Given a number and the task is to convert the number from decimal to hex. This can be done by using toString() method. It takes the parameter which is the base of the converted string. In this case, the base will be 16. Syntax: decimalNumber.toString( radix ) Parameters: decimalNumber: It holds the 1 min read How to convert binary to decimal in JavaScript? A binary number is a number expressed in the binary numeral system, which uses only two symbols 0 and 1. Decimal numbers are base 10 in a JavaScript number system that uses 10 digits from 0 to 9. We are given a number as Binary input we have to convert it into decimal in JavaScript and print the res 3 min read Octal to Decimal conversion using JavaScript An octal number is a number system that uses a base of 8 and numbers are represented using the digits 0 through 7. Decimal numbers are a base 10 number system which uses 10 digits from 0 to 9. We are given a number a octal number and we have to convert it into decimal in JavaScript and print the res 3 min read How to convert string into float in JavaScript? In this article, we will convert a string into a float in Javascript. We can convert a string into a float in JavaScript by using some methods which are described below: Methods to Concert String into Float:Table of Content Method 1: By using Type Conversion of JavaScriptMethod 2: By using parseFloa 6 min read How to Validate Decimal Numbers in JavaScript ? Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N 2 min read How to Remove the Decimal Part of a Number in JavaScript ? The decimal portion of a number in JavaScript can be removed through various methods, including the Math. trunc( ) method, Math. floor( ) method, Math. ceil( ) method, and Math. round( ) method. SyntaxMath.trunc(11.5) // Output: 11Math.floor(12.5) // Output: 12Math.ceil(15.5) // Output: 15Math.round 2 min read Like