JavaScript prompt Box

« Previous Tutorial Next Tutorial »

The prompt box in JavaScript contains a text box along with OK and Cancel buttons. It returns the text string when OK is clicked and null when Cancel is clicked.

JavaScript prompt Box Example

Here is an example demonstrates prompt box in JavaScript:

   <title>JavaScript prompt Box</title>

<h3>JavaScript prompt Box Example</h3>
<script type="text/javascript">
   var myname = prompt("Please enter your name: ");
   if(myname == null || myname == "") myname = " visitor ";
      document.write("Hi " + myname + ", Welcome to");


Here are some sample runs of the above JavaScript prompt box example. This is the initial output:

javascript prompt box

Now, enter your name as shown in the following snapshot:

javascript prompt box example

After entering your name, just press OK button. Here is the output produced after pressing the OK button:

prompt box javascript

In case, if your forgot to fill the text box and just pressed OK button, then here is the sample output produced after performing this action:

prompt box

JavaScript Online Test

« Previous Tutorial Next Tutorial »

© Copyright 2021. All Rights Reserved.