Back

Javascript

Alerts

Example 1

Create a new document. Place the following code in the body of your document. Then save and preview. What happens?

<script type="text/javascript">
alert("Hello, world!");
</script>

Example 2

Change the "Hello, world!" to numbers, like the ones below. Then save and preview. What happens?

<script type="text/javascript">
alert("2+2");
</script>

Example 3

Remove the quotation marks, like in the example below. Then save and preview. What happens? Try removing the quotation marks from "Hello, world!" in Example 1.

<script type="text/javascript">
alert(2+2);
</script>

Example 4

Try including both letters and numbers, like in the example below. Then save and preview. What is the "+" doing? Try removing it to see what happens.

<script type="text/javascript">
alert("Here is a number: " + 2);
</script>

Example 5

Try the example below. Then save and preview. Can you explain the result?

<script type="text/javascript">
alert("Here is a sum: " + 2 + 2);
</script>

Example 6

Try the example below. Then save and preview. Does this work better? How could you add a period at the end?

<script type="text/javascript">
alert("Here is a sum: 978 + 2369 = " + eval(978 + 2369) );
</script>

Example 7

Move the code into the head of your document. Then save and preview. What happens?