Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - ParseInt(prompt) is sometimes used in js, and prompt () is sometimes used only. Why?
ParseInt(prompt) is sometimes used in js, and prompt () is sometimes used only. Why?
ParseInt can convert the return value of prompt into a numerical value, otherwise prompt will return a string (even if a number is entered). In most cases, js will automatically type the data as needed, but sometimes it will make mistakes. For example, when two numbers in string format are added, js will merge strings instead of mathematical operations, such as

" 123" + "456" = " 123456"

Therefore, if you want prompt to input a number that can be used for operation, you need to use parseInt for forced type conversion, so as to avoid program errors (if it is a number with a decimal, such as an amount, use parseFloat instead).

If prompt enters a non-numeric string (such as name, address, etc.). ) or numbers that do not need to participate in mathematical operations (such as ID number, mobile phone number, order number, etc. ), don't use parseInt, or you will make a mistake!