Expert Developer JavaScript Question: Download Expert JavaScript Developer PDF
What value does prompt() return if the user clicked the Cancel button?
Answer:
Return value of prompt() function depends on browsers. Most of the browsers return the value as null and some return as empty string (" "). IE is one of the browser which gives the error of empty string when clicked the cancel button by the user, otherwise all the recent browser return the value as null. The code to check this is as follows:
userInput = prompt('Prompt text','Suggested input');
if (userInput) {
// do something with the input
}
userInput = prompt('Prompt text','Suggested input');
if (userInput) {
// do something with the input
}
Previous Question | Next Question |
What are the different types of errors in JavaScript? | What is Push() method in JavaScript? |