Skip to main content

Browser Window Interaction

JavaScript Fundamentals: Interaction: alert, prompt, confirm

What is the JavaScript alert window commonly called in the JavaScript community?

View Answer:
Interview Response: Web developers refer to this type of window as a Modal Window.

How does the alert function work?

View Answer:
Interview Response: It shows a message and waits for the user to press “OK” and returns a string as a value.

What data type does the alert window method return?

View Answer:
Interview Response: Alert returns a string.

Technical Response: Alert returns a string or object converted into a string and displayed.

How does the prompt function work in simple terms?

View Answer:
Interview Response: It shows a window with text and a request for input, then returns a string.

Technical Response: It shows a modal window with a text message, an input field for the visitor, and the buttons OK/Cancel. The return value of the prompt function is a string value, which should be taken into account when used with numbers.

How many arguments does the prompt function accept?

View Answer:
Interview Response: Prompt accepts two arguments the title and a default value. The default value is not required and acts as the initial value for the input field.

Code Example:

// the brackets denote that the parameter is not required 

result = prompt(title, [default]);

What does the prompt return when the prompt its escaped or canceled?

View Answer:
Interview Response: It returns a null value when it's escaped or canceled.

Why is it essential to provide a second argument (default) for the prompt function?

View Answer:
Interview Response: This approach ensures that the browser (internet explorer) does not return undefined.

Technical Response: There is a chance that the user is using a browser such as Internet Explorer, which returns undefined if there is no default. This action could have ramifications that could affect the application adversely.

How does the confirm function work?

View Answer:
Interview Response: Confirm produces a window with a Boolean question of OK and Cancel. Ok returns true, and Cancel returns false.

Technical Response: The confirm function shows a modal window with a question and two buttons: OK and Cancel. The result is true if OK is pressed and false otherwise.

Code Example:

let isBoss = confirm('Are you the boss?');

alert(isBoss); // true if OK is pressed and false otherwise

The alert, prompt, and confirm methods are part of what object model?

View Answer:
Interview Response: The alert, prompt, and confirm methods belong to the BOM.

Technical Response: The alert, prompt, and confirm methods belong to the Browser Object Model. It is commonly called the BOM.

What are two limitations shared by the alert, prompt, and confirm methods?

View Answer:
Interview Response: We have no control over the position and look of the modal window.

Technical Response:

  1. The browser determines the exact location of the modal window. Usually, it is in the center.
  2. The exact look of the window also depends on the browser, and we cannot modify it.