Expert Developer JavaScript Question: Download Expert JavaScript Developer PDF

What value does prompt() return if the user clicked the Cancel button?

Tweet Share WhatsApp

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
}

Download Expert JavaScript Developer PDF Read All 150 Expert JavaScript Developer Questions
Previous QuestionNext Question
What are the different types of errors in JavaScript?What is Push() method in JavaScript?