Data conversion in Java Script

How to convert data into another datatype in Java Script. In this Javascript practice exercise, we will see some examples to covert Binary data into decimal,

Convert binary number to decimal in JavaScript

Write a function that converts binary numbers to decimal numbers using javascript. It is easier for us when we using the parseInt() function java.

Steps

  • Initialize binary number
  • Use function parseInt();
  • Display output

Js code to Convert binary number to a decimal.

<!DOCTYPE html>
<html>
<head>
  <title>Convert Decimal to Binary</title>
</head>
<body>
  <script type="text/javascript">
    var binary = "101";
var decimal = parseInt(binary, 2);
alert(decimal);
  </script>
</body>
</html>

Output

In this way, we learned how to convert binary numbers to decimals.

JS code to convert Binary to decimal without using parseInt()

<!DOCTYPE html>
<html>
<head>
  <title>Convert Decimal to Binary</title>
</head>
<body>
  <script type="text/javascript">
    var inp = `11110`;

    var len = inp.length;

    var string = inp.split("");

    var counter = string.map(function(val,i){
     return i;  
    }).reverse();

    var storeBin = string.map(function(val,i){

    let x ="";


    if(val>0){
    x += 2;
    }else if(val===0){
    x += 0;
    }

    return Math.pow(string[i]*x,counter[i]);

    });


    var dec=0; 

    for(i=0;i<len;i++){
    dec += storeBin[i]
    }
   console.log("Binary "+inp);
  console.log("Decimal "+dec);
  </script>
</body>
</html>

Output

This is another way to convert binary to decimal.

Convert Numbers in String in JavaScript.

In this, we have to convert the numbers to string using tostring() method.

Steps

  • take input
  • Display output

JS code to convert numbers to string

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Convert number to string </title>
</head>
<body>
    <script type="text/javascript">
    var num=22;
    document.write("Output : " + num.toString(2));          
</script>
</style>
</body>
</html>

Output

In this way, we learned how to convert numbers to string.

Convert decimals in int in JavaScript

In this, we will see how to convert decimal numbers into an int using the javascript function.

Steps

  • Take input
  •  Use javascript function
  • Display output

Js code to convert decimal numbers into int

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>decimal number to int</title>
</head>
<body>
<script>
   var x = 10.50;
   var z = Math.floor(x);
   document.write("Converted value of " + x + " is " + z);
</script>
</body>
</html>

Output

In this way, we learned how to convert decimal numbers into int.

Convert int to decimal till 2 digits after the dot in JavaScript

In this, we learn how to convert decimal numbers to 2digit after the dot.

Steps

  • Take input
  • Use Parsefloat() function
  • Return number string
  • Display output

JS code to convert decimal till 2 digits after the dot

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Flot number to decimal upto 2 digit</title>
</head>
<body>
<script>
    function ParseFloat(str,val) {
    str = str.toString();
    str = str.slice(0, (str.indexOf(".")) + val + 1); 
    return Number(str);   
}
document.write("Output is -" + ParseFloat("10.547892",2));
</script>
</body>
</html>

Output

In this way, we learned how to convert decimals to int till 2digit after the dot.

Convert Date and time in String

In this, we see how to convert date and time to string in javascript using .tostring() and .todatestring() functions.

Steps

  • Take input
  • Use functions
  • Display output

Js code to covert date and time in string

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        Date and toime to string
    </title>
</head>
<body>
<script>
    const event = new Date(1993, 6, 28, 14, 39, 7);
console.log(event.toString());

console.log(event.toDateString());
</script>
</body>
</html>

Output

In this way, we learned How to convert date and time to string in JS.

Convert Booleans to Numbers JavaScript

In this, we learn to convert Booleans to numbers using the javascript function.

Steps

  • Use function Booleantonumbers()

Js code to covert convert booleans to numbers

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Convert Boolean to Numbers </title>
</head>
<body>
<script>
 var IsLoggedIn=true;

 function fnConvertBooleanToNumber(){ 
     var convertedBoolValue = Number(IsLoggedIn); 
     alert(convertedBoolValue); 
 } 
</script>
<label>Click<label>
<input type="button" value="Convert Boolean to Number" onclick="fnConvertBooleanToNumber()" />
</body>
</html>

Output

In this way, we learned how to convert booleans to numbers in JS.

Convert Numbers to Booleans in JavaScript

In this, we convert Numbers to Booleans using JS functions.

Steps

  • Use Js Functions

Js code to covert numbers to booleans

<!DOCTYPE html>
<html>
  
<body>
    <center>
  
        <h4>
            Click  to change the
            number into boolean.
        </h4>
  
        <button onclick="myFunction()">Change</button>
  
        <p>The number value of the variable is :</p>
  
        <p id="result"></p>
  
        <script>
  
            var numvalue = 1;
  
            function myFunction() {
  
                document.getElementById("result")                     .innerHTML = Boolean(numvalue);
            } 
        </script>
    </center>
</body>
  
</html>

Output

 

In this way, we learned how to convert numbers to booleans.

Covert array list into a JSON object in JavaScript.

In this, we learn how to convert an array list into an object using the JavaScript function.

Steps

  • Use JavaScript function

Js code to covert array into a JSON object

<!DOCTYPE html>  
<html>  
    <head> 
        <title> 
            Convert array to JSON.
        </title>
    </head> 
    <body style = "text-align:center;" id = "body">  
 
        <p id = "ATJ_UP" style = "font-size: 16px;">
          
        </p>
        <button onclick = "gfg_Run()"> 
            Convert
        </button>
        <p id = "ATJ_DOWN" style = "color: green; 
            font-size: 20px; font-weight: bold;">
        </p>
        <script>
            var el_up = document.getElementById("ATJ_UP");
            var el_down = document.getElementById("ATJ_DOWN");
            var array = [34, 24, 31, 48];
            el_up.innerHTML = "Array = [" +array+"]";;
            function gfg_Run(){
            el_down.innerHTML = "JSON_String = '"+JSON.stringify(array)+"'";  
            }           
        </script> 
    </body>  
</html>

Output

In this way, we learned how to convert array lists to JSON objects.

Convert JSON object into an array in JavaScript

In this, we learn how to convert  JSON objects into an array in JS.

Steps

  • Use JS functions

Js code to convert JSON object into an Array

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        Convert JSON string to array
        of JSON objects using JavaScript.
    </title>
</head> 
  
<body style = "text-align:center;"> 
      
    <p id = "GFG_UP"></p>
      
    <button onclick = "myGFG()">
        Click Here
    </button>
      
    <p id = "GFG_DOWN"></p>
      
    <script>
        var up = document.getElementById("GFG_UP");
          
        var JS_Obj = 
        '{"prop_1":"val_1", "prop_2":"val_2", "prop_3" : "val_3"}';
          
        up.innerHTML = "JSON string - '" + JS_Obj + "'";
          
        var down = document.getElementById("GFG_DOWN");
          
        function myGFG() {
            var obj = JSON.parse(JS_Obj);
            var res = [];
              
            for(var i in obj)
                res.push(obj[i]);
              
            down.innerHTML = "Array of values - ["
                            + res + "]";
        }
    </script> 
</body> 
  
</html>

Output

In this, we learned how to convert the  JSON object into an array.