ESLint Unexpected use of isNaN

前端 未结 5 1032
滥情空心
滥情空心 2021-01-30 09:47

I\'m trying to use the isNaN global function inside an arrow function in a Node.js module but I\'m getting this error:

[eslint] Unexpected use of \'is

5条回答
  •  醉酒成梦
    2021-01-30 10:20

    In my case, I wanted to treat 5 (integer), 5.4(decimal), '5', '5.4' as numbers but nothing else for example.

    If you have the similar requirements, below may work better:

    const isNum = num => /^\d+$/.test(num) || /^\d+\.\d+$/.test(num);
    
    //Check your variable if it is a number.
    let myNum = 5;
    console.log(isNum(myNum))
    

    To include negative numbers:

    const isNum = num => /^-?\d+$/.test(num) || /^-?\d+\.\d+$/.test(num);
    

    This will remove your issue of global use of isNaN as well. If you convert the isNum function to a normal ES5 function, it will work on IE browser as well.

提交回复
热议问题