A diferencia de otros lenguajes donde hay una clasificación de tipos de datos numéricos (coma flotante, enteros…), en Javascript sólo tenemos un tipo de dato number. La forma de crear variables con valores numéricos es la siguiente.
Ejm
let num = 1; let num2 = new Number(2); console.log(num, num2);
operador typeof()
El operador typeof() nos dice de qué tipo de dato es una variable.
Ejm
console.log(typeof num, typeof num2); // NOS INDICA EN CONSOLA EL TIPO DE DATO DE LAS VARIABLES
Veamos un ejm completo.
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Numbers</title> </head> <body> <h1>Numbers in Javascript</h1> <script> let a = 1; let b = new Number(2); let c = 7.14; let d = "5.6"; console.log(a); console.log(b); // toFixed() REDONDEA AL NÚMERO DE DECIMALES QUE LE PASEMOS COMO PARÁMETRO // parseInt() DEVUELVE SÓLO LA PARTE ENTERA console.log(c.toFixed(1)); console.log(parseInt(d)); console.log(typeof c, typeof d); console.log(c + parseInt(d)); </script> </body> </html>