Expert Developer JavaScript Question:
Download Questions PDF

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

Expert JavaScript Developer Interview Question
Expert JavaScript Developer Interview Question

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 Interview Questions And Answers PDF

Previous QuestionNext Question
What are the different types of errors in JavaScript?What is Push() method in JavaScript?