JavaScript’s window.alert() method is used to display an alert box with a specified message and an OK button. It’s a simple way to show information to the user or get their attention.
Here’s how to use window.alert():
window.alert("Hello, World!");
This will display an alert box with the message “Hello, World!”.
You can also use variables or expressions inside the alert:
let name = "Mr Pons";
window.alert("Hi, " + name + "!");
This will show an alert with the message “Hi, Mr Pons!”.
Note that window.alert() is a method of the window object, which represents the browser window. In most cases, you can omit ‘window’ and simply use alert():
alert("This works too!");
Remember that alert boxes are synchronous and block the execution of the script until the user clicks OK. They’re useful for debugging or simple notifications.