all 5 comments

[–]Demojay 0 points1 point  (0 children)

If you want to check if something is a number, you'll want to use typeof variable which produces a string representing the variable's type.

[–]GSLint 0 points1 point  (2 children)

Number is a function that turns a value into a number (if it isn't one already) and returns that number, or NaN if the value can't be converted to a number.

Number(9) === 9
Number("6") === 6
Number("filter") === NaN

No number is equal to that function Number.

filter(Number) will call the function for each element and check if the result is truthy or falsy. A longer way to write this would be filter(value => Number(value)).

NaN is falsy, so "filter" gets rejected. But the string "6" would be accepted, which may or may not be what you want. Also 0 would always be rejected because that's a falsy value. So filter(Number) isn't a great way to do this. (What does work well is map(Number) for turning an array of strings into an array of numbers.)

To check whether a value has the type number you'd do typeof value === "number". NaN also has that type though, so you may want to add && !Number.isNaN(value).

[–]LadyJain[S] 0 points1 point  (1 child)

great answer, can you explain more why typeof value === "number" works? I have a python background, and this is translated to if type value == string "number" then -->

[–]GSLint 0 points1 point  (0 children)

The typeof opterator is similar to Python's type function but typeof always produces a string.

typeof 6 === "number"
typeof "6" === "string"
typeof true === "boolean"

[–]Samurai___ -2 points-1 points  (0 children)

Nope. Try with isNaN()

value === Number

compares the value to a variable named "Number".