Previous Page : Introduction To JavaScript

JavaScript provides several ways to output data to the user. The most common methods are:

  1. console.log() : This method is used to output data to the browser's console. It's mainly used for debugging purposes, as the user won't see the output unless they open the browser's developer tools.

    console.log("Hello, JavaScript!");
    
  2. document.write() : This method writes the specified content to the current document. It can be used to write text or HTML to the webpage. However, it's not recommended to use this method in modern web development as it will overwrite the entire content of the page.

    document.write("Hello, JavaScript!");
    
  3. innerHTML: This property sets or returns the HTML content within an element. It can be used to change the content of an HTML element on a webpage.

    <p id="demo"></p>
    
    <script>
      var demo = document.getElementById("demo");
      demo.innerHTML = "Hello, JavaScript!";
    </script>
    
  4. alert(): This method is used to display an alert box with a message to the user. It's commonly used for error messages or to alert the user to a specific action.

    alert("Hello, JavaScript!");
    
  5. prompt(): This method is used to display a dialog box that prompts the user for input. It returns the input value as a string.

    var name = prompt("What is your name?");
    alert("Hello, " + name + "!");
    
  6. document.querySelector() and document.querySelectorAll(): These methods are used to select one or more elements on a webpage, respectively. Once an element has been selected, it can be manipulated using properties such as innerHTML or by adding event listeners.

    <p class="demo">Hello, JavaScript!</p>
    
    <script>
      var demo = document.querySelector(".demo");
      demo.innerHTML = "Hello, World!";
    </script>
    

These are the most commonly used methods for outputting data in JavaScript. However, there are many other ways to output data depending on the specific use case.

JOIN WHATSAPP COMMUNITY FOR MORE SUCH AWESOME CONTENT :

<aside> 💡 CLICK HERE TO JOIN

</aside>

Join Instagram Community ( 80k+) : @webdevarmy & @cssdevarmy

Youtube : @webdevarmy