Getting to Learn Window Objects in JavaScript - alert() and prompt() Methods

Guest Posts Paolo Nikko Nuñal

JavaScript boasts a lot of methods. In this tutorial we are going to deal with window object methods from JavaScript. Most of the time you might be curious how does pop-up open up on your browser. We'll in this tutorial you will learn how to do it, and it is easily done with one line of code.

In this tutorial you will learn how to give users notification using the alert() and prompt() method.

One of the most common information boxes that you will encounter most especially if you are inserting something in a database and there will be a prompt that you have successfully inserted that item.

Syntax:

alert([MESSAGE]);

MESSAGE - (OPTIONAL) Specifies the text you want to display or an object that will be converted to a string and displayed when called.

Code:

alert("Hello World");


Another one is the prompt() method that lets you ask your user to input something. This is an alternative if you don't want to create a modal.

Syntax:

prompt([TEXT],[DEFAULT TEXT]);

TEXT - (REQUIRED) The text you want to display when called.

DEFAULT TEXT - (OPTIONAL) The default input value.

Code:

<script>
     var answer = prompt("What is your name");
     alert("Your name is: " +answer);
</script>


Base from the code above, we have a variable answer in which it stores whatever the use inputs on the the prompt.