Search code examples
javascriptif-statementfloating-pointconditional-statementstypeof

How can i represent a float in JavaScript?


I'm a newbie at JavaScript.

In python, I can create a conditional that checks if a number is a float like this:

num = 1.5

if type(num) == float:
    print('is a float')

How can i do it in JavaScript? It is possible?


Solution

  •     you can try:
        
        let x=1.5;
    if(!Number.isInteger(x)) {
      console.log(`The number ${x} is a float`)
    };
        
        or
        
    function checkFloat(x) {
      //Check if the value is a number
       if(typeof x == 'number' &&  !isNaN(x)) {
          //Check if is integer
         if(Number.isInteger(x)) {
         //print the integer
            console.log(`${x} is integer`);
          }else {
            //print the float number
            console.log(`${x} is a float`);
          };
       }else {
         //print the value that is not a number
          console.log(`${x} is not a number`);
        };
    };