How to return a number if possible, else a string. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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 ответов
+ 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