0

How to return a number if possible, else a string.

Is it possible (in Javascript) to create a function with an argument n that returns the number n if it's possible to convert it to a number, else the string n? Example of what i mean : numberIfPossible("4"); //returns the number 4 numberIfPossible("text"); //return the text "text"

24th Oct 2019, 2:51 PM
somebody
somebody - avatar
2 Réponses
+ 3
yes. Use the isNaN function.
24th Oct 2019, 3:02 PM
Airree
Airree - avatar
0
That worked. Thank you.
24th Oct 2019, 3:04 PM
somebody
somebody - avatar