• Related Post

    Monday, January 22, 2018

    JavaScript Output, JavaScript Display Output Possibilities

    JavaScript Output

    JavaScript does not have any built in capabilities to print or display. It uses browser’s window and document object for display or print. So today i am going to teach you about JavaScript output. JavaScript can display your data in many different way. Mostly there are four ways to display data in JavaScript.

    JavaScript Display Output Possibilities

    innerHTML:- For writing into HTML element.
    document.write():- For Writing into HTML for Output using.
    window.alert():- Writing for an alert box
    console.log():- For writing into browser console.


    Using innerHTML Method

    <html>
    <body>

    <p id="demo"></p>

    <script>
    document.getElementById("demo").innerHTML = 10 + 5;
    </script>

    </body>
    </html>

    In this example to access HTML element we use the document.getelementByid method. And the ID defines the HTML element. The innerHTML property define the HTMl content.

    Using document.write() Method

    <html>
    <body>

    <h1>My First Web Page</h1>
    <p>My first paragraph.</p>

    <script>
    document.write(10 + 5);
    </script>

    </body>
    </html>

    It displays output in the document itself. It is generally used for testing and banners,

    window.alert() Method

    <html>
    <body>

    <h1>My First Web Page</h1>
    <p>My first paragraph.</p>

    <script>
    window.alert(10 + 5);
    </script>

    </body>
    </html>

    Alert box is used to display data to user in separate modal dialog. You need to press OK to continue.

    console.log() Method

    <html>
        <body>
            <h1>My Web Page</h1>
            <p >My Paragraph</p>
            
            <script>
            console.log(10 + 5);
            </script>
        </body>
    </html>

    All modern browsers come with a in-built console. console.log() method is used to display data in console. F12 key in browser opens the console. Console is very useful in debugging and testing JavaScript code. In Console pane there is a box at right side where you can write any JavaScript code and execute it without reloading a page.


    No comments:

    Post a Comment