Is it good to use +prompt
instead of just regular prompt
in JavaScript if I want only integer numbers to be typed in my prompt window? Does it say something like that about +prompt
in the JavaScript standard/rules ?

- 41,009
- 21
- 145
- 105

- 483
- 6
- 17
-
1What is `+prompt`? – Sandeep Nayak Jun 24 '16 at 10:32
-
2@SandeepNayak if I enter some strings into the prompt when using +prompt it will return NaN. – Newcomer Jun 24 '16 at 10:33
-
2@SandeepNayak — http://stackoverflow.com/questions/15129137/what-does-mean-in-javascript – Quentin Jun 24 '16 at 10:33
5 Answers
Well this is what happen's when you add plus before prompt, i.e. as below,
Eg :- 1
var a = prompt("Please enter a number");
console.log(a);
typeof(a);
Now in eg (1) when you enter a number and if you check that in console, it show a number but as that number is in-between double-quote, so in JavaScript it's a string, that's what it will show in typeof too when you console that.
Eg :- 2
var a = +prompt("Please enter a number");
console.log(a);
typeof(a);
Now when you console the var a and typeof
a of eg(2) the result differs as we have added + before prompt. So this time we get our prompt input value as number and not string. Try you will understand what I'm saying.

- 8,455
- 2
- 22
- 25
No.
The unary plus operator will convert the response in to a Number, not an integer.
It could give you a floating point value, it could give you NaN
.
If you want an integer then you need to check the response and then put in some error recovery for cases where the response is not what you want.
For example: If it is a floating point value, then you might want to just use Math.floor
to convert it. If it is NaN
then you might want to prompt
the user again.
The effect of +promt("...")
is that the result of the promt command will be cast to a number.
This is a nice hack, but not a clean solution.
I would recommend to assign the user input to a variable, then check it and in case it doesn't match the requirements, throw an exception or error message.
var
input = prompt("Please enter a positive number"),
inputNum = parseInt(input, 10);
if (isNaN(inputNum) || inputNum < 1)
alert("You did not enter a positive number.");

- 12,375
- 12
- 51
- 73
-
1"to a positive number" — Incorrect. It doesn't force a positive number. `+"-22.134"` evaluates as `-22.134` – Quentin Jun 24 '16 at 10:38
-
@Quentin is right, it just coerces the variable to a number but does not enforce any sign (don't be fooled into thinking the + operator has anything to do with positive numbers). You should correct this as your SO score gives your post high credibility and can more easily lead to the misinformation spreading. – kb. Jun 24 '16 at 10:43
-
1
So putting a +
before any data type converts it into a number.
I tried this:
typeof(+"100") ==> number
typeof(+"12.34") ==> number
typeof(+true) ==> number
typeof(+false) ==> number
Things got really weird when I experimented with the undefined
data type. e.g.:
x = +undefined
typeof(x) ==> Number whilst value of variable x is NaN

- 15,079
- 5
- 45
- 61

- 141
- 1
- 1
- 3
-
That makes sense. `parseFloat(undefined)` is NaN, just like `parseFloat('afije')` But NaN is of type number. – RoboticRenaissance Oct 20 '21 at 15:59
+prompt()
is just a +
before a prompt()
, it's like writing +"3"
or +"10"
. It just tries to cast the outcome to a number.

- 4,009
- 2
- 16
- 27