← Volver

How to Check if a Variable is a Number

4 mayo 2019 1 min lectura
Sunset in the mountains

We can check if a variable is a number in multiple ways.

isNaN

We can check by calling isNaN with the variable as the argument. It also detects if a string’s content is a number. For example:

isNaN(1) // false
isNaN('1') // false
isNaN('abc') // true

Note: isNaN(null) is true .

typeof Operator

We can use the typeof operator before a variable to check if it’s a number, like so:

typeof 1 == 'number' // true
typeof '1' == 'number' // false

Avatar

Daniel Carmona | Blog

El lugar donde lo ilógico se puede convertir en razonable y donde las preguntas guían tu camino. También encontrarás cosas de programadores y de gente que monta en bici. Siéntete libre de escribirme para cualquier sugerencia, reporte o cualquier cosa que se te pase por la cabeza