Why are answers from variable prompts strings in JavaScript no matter what I enter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why are answers from variable prompts strings in JavaScript no matter what I enter

Every time I enter a specific number, the outputs are shown as strings, as the same type as inputing some text. Why does it works in such way?

4th Jan 2019, 11:45 AM
Hou, Bo-Rui (侯柏瑞)
Hou, Bo-Rui (侯柏瑞) - avatar
3 Answers
+ 5
As mentioned already, prompt returns a string of text you entered. Aside from eval(), you can also use Number() and parseInt() for integers and parseFloat() for floating point numbers. https://code.sololearn.com/W748XyDSiKdd/?ref=app
4th Jan 2019, 1:30 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 3
I think its because the prompt method is of type String
4th Jan 2019, 11:59 AM
D_Stark
D_Stark - avatar
+ 1
because prompet function return a string value.. but you can use (eval) such: x=eval(prompt("x=","0"));
4th Jan 2019, 12:27 PM
Semsem