I want a method which if given a hexadecimal number (e.g. 0x01) returns true. I tried with a string argument, but I need number type check! For example:
isHex(0x0d) //-> true
isHex(12) //-> false
This is not possible as hexadecimal number literals are just another way of representing the same number (translated during parsing) and there’s no way to differentiate them:
0x10 //=> 16
0x10.toString() //=> '16'
typeof 0x10 //=> 'number'
typeof 16 //=> 'number'
This would only be possible if you passed the literal as a string:
function isHex(num) {
return Boolean(num.match(/^0x[0-9a-f]+$/i))
}
isHex('0x1a') //=> true
isHex('16') //=> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With