Table of contents
1.
Introduction
2.
Alert
3.
Prompt
4.
Confirm
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Interaction: Alert, Prompt, Confirm

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Here we look at various Javascript Functions that help the browser to interact with the user. Here we deal with an alert, prompt, Confirm javascript function.

Also Read About, Javascript hasOwnProperty

Alert

This is displayed when we need user confirmation. i.e. a sequence of happening will not happen until the user gives ok assurance on the alert prompt displayed by the user.

We can call the alert function in majorly two ways they are:

The first way is using:

Code:

 window.alert("hello World");


Output:

The Second Way is:

Code:

 alert("hello  World");


Output:

Once we write the above code in the Console javascript, the modal window is displayed at the top, which is user interactive. It asks for the user confirmation for running the further operation.

Prompt

The function prompt is used to take input from the modal window and display the message after we press ok on the modal window;

Working:

The working of the prompt function is achieved in such a way that it First displays the text message on the modal window, then takes the user's input, and then displays the message.

Code:

let getage = prompt('How old are you? This is the Message', 100);//100 is the default value
alert(`You are ${getage} years old!`);


 Output:



 

Explanation :
In Image 1, we display the message to the user to enter the value with the default value given to be 100, then in image 2, we add our custom value and then press the ok button. Then, in image 3, we are getting the desired output.

Confirm

The confirm function is used to get the boolean input, i.e. if ok is pressed at the modal window that it returns true else, it returns false.

Code:

let checkBoss = confirm("Are you the boss at coding ninjas ?");
alert( checkBoss );


Output:


Explanation :
Here, image one displays the format in the modal window that will ask for the boolean value, and image-2 explains that if we press ok on the modal window, it returns a true value.

You can practice by yourself with the help of an online javascript compiler.

FAQs

  1. What are three specific functions to interact with the user?
    Three specific functions are:
    Alert
    Prompt
    Confirm
     
  2.  What is the use of the alert Function?
    Alert function is used for displaying the message on the modal window.
     
  3. What is the use of the Prompt function?
    Prompt function is used to take the input from the user in a modal window.
     
  4. What is the use of the Confirm function?
    Confirm function is used to take the boolean response from the user in the form of ok(for true) and Cancel (For false);
     
  5. Can we assign the Define Default value to the Prompt function?
    Yes, we can assign a Default value to the Prompt function;

Key Takeaways

Javascript provides the three functions that permit the browser to interact with the user. The interaction could be simply displaying text messages or asking for any type of input from a user, or the interaction could be a boolean type, i.e. in the form of ok or Cancel.

Coding Ninjas Self paced course Full stack developement help the user to master all these concepts on Web-devlopement and for practising Questions on it Visit Coding Ninjas-Coding Ninjas Studio.

Live masterclass