How to display error without alert box using JavaScript?

Errors in JavaScript can be displayed without the use of alert boxes but using the alert box is the traditional way to do that.

How to display error without alert box using JavaScript?

console.log(); is very popular for debugging JavaScript.
Just remember to remove them before the code goes live.

Another way is to have a log area using a readonly textarea. That way you can easily update it by adding more logs to it.

For example, at https://jsfiddle.net/a4c0fgu9/1/

<p><button id="button">Press Button</button></p>
<textarea id="logarea" cols="40" rows="10" readonly></textarea>
function log(message) {
  const logArea = document.querySelector("#logarea");
  const date = new Date().toString().match(/(\d+:\d+:\d+)/)[1];
  const logMessage = date + " " + message;
  logArea.value = (
    logArea.value
    ? logArea.value + "\n" + logMessage
    : logMessage
  );
}
document.addEventListener("DOMContentLoaded", function () {
  log("DOMContentLoaded event");
});
window.addEventListener("load", function () {
  log("Page loaded");
});
log("Script run");

document.querySelector("#button").addEventListener("click", function () {
  log("button clicked");
})
setTimeout(function () {
  log("Timeout delay occurred");
}, 2000);

Then there’s libraries such as Bootstrap, that gives you a wide variety of ways to show on-page alerts.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.