Javascript – Show a message to user

Javascript is scripting language and it is a client side scripting language. Javascript is having a function using which we can print a message. Unlike, c, c++ or java, where we print message into the console, in Javascript there is no such console. Therefore, to show a message we need to add extra layout to the webpage. Thankfully to javascript, this job has been done so easy. All we need is to add call the function and rest will be done.

Show Hello World using Javascript to the user:

The function which we use to show message is “alert”.

alert(“message”);

Lets show “hello world” to the user using javascript:

For this we are going to use xampp, and going to create html page and in this html page we are going to use javascript.

<html>

<head>

<title>This is a sample webpage</title>

<meta name=”content” description=”this is a sample webpage for practice purpose.”/>

</head>

<body>

This is a sample webpage. This webpage is for practice purpose. I have successfully developed the webpage.

</body>

</html>

If you are not aware of how to download, install xampp, then read: How to download and install xampp in Mac OS and Windows

If you are not aware of how to launch xampp, then read: How to launch xampp in Mac OS and Windows

If you are not aware how to create project inside xampp server, then read: How create project in xampp

To use javascript inside html page, we need to use script tag.

<html>

<head>

<title>This is a sample webpage</title>

<meta name=”content” description=”this is a sample webpage for practice purpose.”/>

</head>

<body>

<script>

//codes

</script>

</body>

</html>

Note: Inside script tag, we keep all javascript codes.

Note: Earlier, we need to mention the type of script we are using : <script type=”text/javascript”>. But current browsers can understand from <script> tag, it is a javascript.

Lets print the message.

<html>

<head>

<title>This is a sample webpage</title>

<meta name=”content” description=”this is a sample webpage for practice purpose.”/>

</head>

<body>

<script>

alert(“Hello world”);

</script>

</body>

</html>

Save the html file and open, it will show the message: ‘hello world’ as below:

Javascript alert function show hello world message

Note: alert is function from javascript to show a message to the client (user)

Alert function is useful in lots of cases where we may want to alert the user.

Example:

Suppose a user needed to provide an email in a box but he has not provided a proper email then we can call alert function and inform the user about the mistake.