Advertisement
Tech

Alerts and Functions in JavaScript - Using Alerts, Prompts, and Confirm Boxes

This guide will explain how you can use different types of alert boxes in your JavaScript code to make it more interactive. It also explains what functions are and how they can be utilized in JavaScript.

By Finn Orfano
Desk Tech
Reading time 3 min read
Word count 487
Web development Internet Javascript help
Alerts and Functions in JavaScript - Using Alerts, Prompts, and Confirm Boxes
Advertisement
Quick Take

This guide will explain how you can use different types of alert boxes in your JavaScript code to make it more interactive. It also explains what functions are and how they can be utilized in JavaScript.

On this page

JavaScript Alerts

In this tutorial, I will explain all there is to know about alerts and prompts in JavaScript. We will also learn about functions, including how and when they should be used.

Alert, Confirmation, and Prompt boxes are the most basic kinds of interactivity elements you can add to your JavaScript code.

Advertisement

Alert Boxes:

Alert boxes are to be used when you just want to pass on some information to the user. The user will be alerted with an alert box with whatever information you want to pass on and then he can click OK and go back to the page. The Alert box doesn’t return any values, unlike the Confirm and Prompt boxes.

Advertisement

The standard syntax for an alert box is

alert(“Hi, This is an alert box”);

Advertisement

Confirm Boxes:

Confirm boxes are generally used when you need confirmation from the user regarding some specific action. The confirmation box is displayed with text on it and the user can then click OK or Cancel. The return value is stored in a variable. If the user clicks OK, the value returned is true. If he clicks Cancel, the value returned is false.

Advertisement

Syntax for Confirm Boxes:

var returnvalue = confirm(“Hi, This is a confirmation box”);

Advertisement

Prompt Boxes:

While alert boxes are used to notify the user and confirm boxes are used to get a yes / no response, prompt boxes can be used to get text input from the user. The prompt box can be used to display information to the user and get a response. It has two buttons - OK and Cancel, and a textbox. If the user enters some text and presses OK, the input is stored in the return variable. If the user clicks Cancel, a null value is returned. You can also specify a default value in the textbox.

Advertisement

Basic Syntax for Prompt Boxes:

var returnvalue = prompt(“Hi, This is a Prompt box”, “This is the default value of the prompt box”);

Advertisement

Here is some code which will demonstrate all types of alert boxes.

Advertisement

Advertisement

Advertisement

Functions:

Functions are blocks of code which are executed when an event triggers the function or when the function is called in the code after its definition. A function can be called at any point after it is defined. It can return values and accept parameters to operate on. Functions are generally used when the same code block is to be used at many points in the program.

Syntax:

function functionname(variable1, variable2)

{

function body

return returnvariable;

}

Here’s an example of a function

This post is part of the series: JavaScript - Tutorials for Beginners

This collection contains several basic JavaScript tutorials for beginners. It explains fundamental concepts, such as operators, variables, loops, events, errors and conditional statements in JavaScript. It assumes no prior programming knowledge.

  1. Conditional Statements in JavaScript
  2. Looping Constructs in JavaScript
  3. Advanced Operators in JavaScript
  4. Alerts and Functions in JavaScript
  5. Error Handling in JavaScript
Keep Exploring

More from Tech

Filed under
Web development Internet
More topics
Javascript help
Advertisement