JSFiddle - React, Tailwind, and code Playground

by rlemon

JavaScript

'0' // ok
'1' // ok
'-1' // not ok
'-1.1' // not ok
'1.1' // not ok
'abc' // not ok

var tests = [
    '0',
    '1',
    '-1',
    '-1.1',
    '1.1',
    '12abc123',
    '+42',
    '0xFF',
    '5e3'
    ];

function nNumber(n) {
    n = n.toString(); // force the value incase it is not
    var n1 = Math.abs(n),
    n2 = parseInt(n, 10);
    return !isNaN(n1) && n2 === n1 && n1.toString() === n;
}

console.log(tests.map(nNumber));