JavaScript alert() Method

You are Here:

JavaScript alert() Method

The alert() method displays an alert dialog with a specified message and an OK button.

Note: The alert dialog should be used for messages which do not require any response on the part of the user, other than the acknowledgement of the message.

alert() as HTML Attribute Value

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p onclick="alert('Hello world')">Click me</p> </body> </html>

Syntax

alert(message)

Parameter Values

ValueTypeExplanation
messageOptionalSpecifies the text/number to display in the alert box.
Message can either be in string or number.

Browser Support

All are checked by myself

alert()
Browser Desktop Mobile
google chrome yesyes
firefox yesyes
internet Explorer yesyes
edge yesyes
opera yesyes
android webview -yes
samsung internet -yes

alert() inside <script> tag

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p onclick="myFunction()">Click me</p> <script> function myFunction(){ alert("Hello world"); } </script> </body> </html>

alert() with Escape Sequence

In the following example, we will use the escape sequence '\n' (new line) to display the following text in new line.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p onclick="myFunction()">Click me</p> <script> function myFunction(){ alert("Hello \nworld"); } </script> </body> </html>

alert() with Property

In the following example, we will using the location.hostname property to display the hostname.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p onclick="myFunction()">Click me</p> <script> function myFunction(){ alert(location.hostname); } </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author