
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Decimal to binary conversion using recursion in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number.
For example −
f(4) = '100' f(1000) = '1111101000' f(8) = '1000'
Example
Following is the code −
const decimalToBinary = (num) => { if(num >= 1) { // If num is not divisible by 2 then recursively return proceeding // binary of the num minus 1, 1 is added for the leftover 1 num if (num % 2) { return decimalToBinary((num - 1) / 2) + 1; } else { // Recursively return proceeding binary digits return decimalToBinary(num / 2) + 0; } } else { // Exit condition return ''; }; }; console.log(decimalToBinary(4)); console.log(decimalToBinary(1000)); console.log(decimalToBinary(8));
Output
Following is the output on console −
100 1111101000 1000
- Related Articles
- Decimal to Binary conversion using C Programming
- Decimal to Binary conversion\n
- How to Convert Decimal to Binary Using Recursion in Python?
- Decimal to binary list conversion in Python
- Program for Binary To Decimal Conversion in C++
- Program for Decimal to Binary Conversion in C++
- Haskell Program to convert the decimal number to binary using recursion
- Swift program to convert the decimal number to binary using recursion
- C Program for Decimal to Binary Conversion?
- Java Program for Decimal to Binary Conversion
- How to convert a number from Decimal to Binary using recursion in C#?
- Conversion between binary, hexadecimal and decimal numbers using Coden module
- Binary to original string conversion in JavaScript
- Swapping adjacent binary bits of a decimal to yield another decimal using JavaScript
- Binary array to corresponding decimal in JavaScript

Advertisements