Javascript output, alert, console.log, innerHTML

Javascript output, alert, console.log, innerHTML
5 (100%) 1 vote

About JavaScript Output

JavaScript does not have built-in functions for display, but we are able to display data in different ways:

  • into an alert box, using alert() method which displays an alert box with a specified message;
    var a = 5;
    var b = 6;
    if (a < b){
      window.alert("a is less then b");
    }
    

     

  • into the browser console, using console.log(). You may pass as many arguments as you’d like;
    var a = 5;
    var b = 6;
    if (a < b){
      console.debug("a is less then b");
      //or
      console.log (a,b);
    }
    

     

  • into an HTML element, using innerHTML property which sets or returns the HTML content of an element;
    var a = 5;
    var b = 6;
    if (a < b){
      document.getElementById("whereToAdd").innerHTML("a is less then b");
    }
    

     

  • into the HTML output using document.write() method.
    If is used after an HTML document is fully loaded, it will delete all existing HTML;
    var a = 5;
    var b = 6;
    if (a < b){
      document.write("a is less then b");
    }
    

     

Leave a Reply

Your email address will not be published. Required fields are marked *