JS DOM - 02.01 - alert, prompt, confirm
Functions used to interact with the user in the browser environment.
All these three methods are modal: they pause the script execution and don’t allow the visitor to interact with the rest of the page until the window has been dismissed.
alert()
It shows a message and waits for the user to press ‘OK’ The mini window is called a modal window.
alert('Hello');
prompt()
It accepts two arguments
result = prompt(title, [default]);
[]
means that the parameter is optional and not needed.
The visitor can type something in the prompt input field and press OK.
Then we get the text in the result
. Or they can cancel the input or esc
, then we get null
as result
.
The value received will be in string format.
let age = prompt('How old are you?', 100);
alert ( `You are ${age} years old`);
confirm()
result = confirm(question);
The function confirm
shows a modal window with a question
and two buttons OK and cancel.
The result
is true
is OK is pressed and false
otherwise.
let admin = confirm("Are you the admin?");
alert(admin); // true if Ok is pressed