1.

How to get the length of a string in JavaScript?

Answer»

To display an alert box in JavaScript, use the alert() method. The alert box would have an OK button.

The syntax:

alert(msg)

Here, msg is the text which is to be displayed in the alert box. SHOWN below under alert():

function show() {   alert("This is an alert box!"); }

The following is an example: 

<!DOCTYPE html> <html> <BODY> <p>Below button displays an alert...</p> <button onclick="show()">Display</button> <script> function show() {     alert("This is an alert box!"); } </script> </body> </html>

The output:

On clicking the above button, the following alert GENERATES and the message we added as parameter is visible:

You MAY be wondering how we can add a multi-line message in an alert box in JavaScript. For that, just use \n as shown below: 

<!DOCTYPE html> <html> <body> <p>Below button displays an alert with multi-line content...</p> <button onclick="show()">Display</button> <script> function show() {     alert("This is first line!\nThis is next line!"); } </script> </body> </html>

The output: 

On clicking the above button, the following alert generates and the multi-line message we added as parameter is visible:



Discussion

No Comment Found