6

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 ?

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
Newcomer
  • 483
  • 6
  • 17

5 Answers5

5

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.

frnt
  • 8,455
  • 2
  • 22
  • 25
4

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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.");
lxg
  • 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
    Thanks, guys. Fixed it. – lxg Jun 24 '16 at 12:33
0

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
marsze
  • 15,079
  • 5
  • 45
  • 61
Deepak Kapri
  • 141
  • 1
  • 1
  • 3
-1

+prompt() is just a + before a prompt(), it's like writing +"3" or +"10". It just tries to cast the outcome to a number.

Bálint
  • 4,009
  • 2
  • 16
  • 27