Make Output By Javascript

Make Output By Javascript


The output is a display of the program that we normally use to show the outcome of the program.

Output in other programming usually displayed in text form with function print ().

There are four ways that I will discuss to display the output from Javascript programs:


First Method: Using console.log function ()

The function console.log () is a function that exist in javascript programming that allows you to display text to the browser console or console javascript.

An example for you please use the following copy the coding below, and try  HERE :

<!DOCTYPE html>
<html>
<head>
<title>Tittle Page</title>
</head>
<body>
<script>
console.log("This is the output from console.log");
</script>
</body>
</html>


Then please click ctrl + shift + J to open the console browser and please run the program, the result will be like this:
Make Output By Javascript
see the output right there in the console, will appear at the output of each coding running


Usually  console.log ()  is used for debugging . Because any error messages that exist in JavaScript is always displayed in the Console.

In addition to console.log () , there are also a number of functions for debugging such console.debug (), console.info (), console.error (), console.dir () , and so on.

Make Output By Javascript
All functions of the console is also true on the console NodeJs.


Method Two: Using functions alert ()

Function alert () is a function that allows you to display a dialog window that we usually see in a pop-up display in a browser. This function is actually located on a Windows object.

The full writing is like this:
window.alert("This is output by Window Alert");
It could also simply alert () just like the following:
alert("This is output by Alert");
Alert function (), only works on any browser. While on Nodejs this function does not exist.


Here is an example of the use of coding functions alert ():

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Learn Javascript</title>
    <script>
        alert("Welcome to Learn Javascript FajarYusuf.Com");
        function sayHello(){
            alert("Good Luck For You!");
        }
    </script>
</head>
<body>
    <button onclick="sayHello()">Klik This Please!</button>
</body>
</html>



The result:
Make Output By Javascript

Third way: Using the function document.write ()

The document object is an object that represents an HTML document in Javascript.

In the document object, there is a function write () to write something into the HTML document.

Example Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Learn Javascript on FajarYusuf.Com</title>
    <script>
        document.write("<h1>I'm Cool</h1>");
        document.write("<hr>");
        document.write("<p>And i learn Javascript</p>");
        document.write("on <b>FajarYusuf.Com</b>")
    </script>
</head>
<body>
</body>
</html>



The result:
Make Output By Javascript

Not only function write (), the object document also provides a wide range of functions for manipulating HTML documents.

Fourth Method: Using innerHTML

innerHTML is an attribute in the (object) HTML element that contains a string of HTML.

By using innerHTML , we can display the output to the more specific elements.

Example Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Learn Javascript FajarYusuf.Com</title>
</head>
<body>
    <h1>Tutorial Javascript Completed</h1>
    <div id="results-output"></div>
    <script>
        //create elemen
        var hasil = document.getElementById("results-output");
        //display output to the result element
        hasil.innerHTML = "<p>This is Output by InnerHTML</p>";
    </script>
</body>
</html>



The result:

Make Output By Javascript
Disqus Comments