Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why lodash .isNumber function is more complicated than typeof value == 'number'

Lodash isNumber function has an extra condition to check if value is number. I'm not sure why is that required and in what case it is not enough to use just typeof value == 'number'

function isNumber(value) {
  return typeof value == 'number' ||
    (isObjectLike(value) && getTag(value) == '[object Number]')
}

https://github.com/lodash/lodash/blob/aa1d7d870d9cf84842ee23ff485fd24abf0ed3d1/isNumber.js

like image 305
dlxeon Avatar asked Dec 11 '22 02:12

dlxeon


1 Answers

From your link:

Checks if value is classified as a Number primitive or object.

var n = new Number(3);
console.log(typeof n); // "object"
console.log(_.isNumber(n)); // true

MDN - Number:

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. A primitive type object number is created using the Number() function.

like image 200
str Avatar answered Feb 16 '23 00:02

str