Previous article: JavaScript Course | Practice Quiz-1
Javascript allows us the privilege with which we can interact with the user and respond accordingly. It includes several user-interface functions which help in the interaction. Let’s take a look at them one by one.
- alert
simply creates an alert box which may or may not have specified content inside it, but it always comes with the ‘OK’ button. It simply shows a message and pauses the execution of the script until you press the ‘OK’ button. The mini-window that pops-up is called the ‘modal window’.alert('text');Example:
// alert example<script>alert('HI there');// with specified contentalert();// without any specified content</script>chevron_rightfilter_noneOutput:

It can be used for debugging or simply for popping something to the user. - prompt
Prompt is another user-interface function which normally contains two arguments.
prompt('text', default value);The text is basically what you want to show the user and the default value argument is optional though it acts like a placeholder inside a text field. It is the most used interface as with it you can ask the user to input something and then use that input to build something.
Example:(with default parameter)<script>// prompt examplelet age = prompt('How old are you?', 50);alert(`You are ${age} years old!`);</script>chevron_rightfilter_noneOutput:

You can enter anything and it will print that, it doesn’t necessarily have to be a number. Without the default value, you have to enter something in the text-field otherwise it will print a blank space simply.
Example:<script>// prompt examplelet age = prompt('How old are you?');alert(`You are ${age} years old!`);</script>chevron_rightfilter_noneOutput:

- confirm
The confirm function basically outputs a modal window with a question and two button ‘OK’ and ‘CANCEL’.confirm('question');Example:
<script>// confirm examplelet isHappy = confirm('Are you Happy?');alert(`You are ${isHappy}`);</script>chevron_rightfilter_noneOutput:

It will print true or false based on your choice of clicking the ‘OK’ button or ‘CANCEL’ button respectively.
Next article: JavaScript Course | Logical Operators in JavaScript
Recommended Posts:
- How to ignore mouse interaction on overlay image using JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Task Tracker Project
- How to check the user is using Internet Explorer in JavaScript?
- Detect the Operating System of User using JavaScript
- How to display hidden element when a user starts typing using JavaScript ?
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- How to Change Image Dynamically when User Scrolls using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


