-2

Although javascript is a weakly typed language, in some scenarios is needed to ensure the data type. Currently many node packages depend on helper functions, e.g, isNumber and isFunction.

It'd be interesting if there were built-in methods for that like Array.isArray. Something like Number.isNumber or Function.isFunction.

What do you guys think about this?

Isaac Ferreira
  • 418
  • 3
  • 16
  • Possible duplicate of [Is there any function like IsNumeric in JavaScript to validate numbers?](https://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers) – stealththeninja Oct 28 '17 at 04:20
  • 1
    [SO] is not a discussion forum, where you ask for opinions. – Sнаđошƒаӽ Oct 28 '17 at 04:25
  • `typeof` already splits hairs between types; Arrays are Object, hence `Array.isArray()` is needed. – dandavis Oct 28 '17 at 06:05

1 Answers1

2

You should not worry much about such. There are already a lot of methods. For your query, you can use Number.isNaN for checking number and use typeof to know if the given variable is a function.

You can check the following answers:

Is there any function like IsNumeric in JavaScript to validate numbers?

How can I check if a javascript variable is function type?


Array is a collection. The collection might refer to [...] or {...} and this is why there's Array.isArray to check if the given collection is array or not. But there's no other way arround for differentiating the function and the number between themselves. And this is perfect reason the javascript should not have such function.

Andrew
  • 7,201
  • 5
  • 25
  • 34
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231