Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 3, 2019 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/09925c54400273f9ebcdc87ffa751da8 to your computer and use it in GitHub Desktop.
Save parzibyte/09925c54400273f9ebcdc87ffa751da8 to your computer and use it in GitHub Desktop.
/**
* Saber si carácter es dígito con JavaScript
*
* https://parzibyte.me/blog
*
*/
const esDigito = (caracter) => {
let ascii = caracter.charCodeAt(0);
return ascii > 47 && ascii < 58;
}
// Probar
const caracteres = ["A","B","0","4","_","9", " "];
caracteres.forEach(caracter => {
console.log("¿'%s' es dígito? %s", caracter, esDigito(caracter));
});
/*
Salida:
¿'A' es dígito? false
¿'B' es dígito? false
¿'0' es dígito? true
¿'4' es dígito? true
¿'_' es dígito? false
¿'9' es dígito? true
¿' ' es dígito? false
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment