How many type of display possibilities in JavaScript?

Asked 19-Jul-2021
Updated 25-Jun-2023
Viewed 270 times

2 Answers


0


Apologies for the confusion in my previous response. In JavaScript, the main ways to display or output information to the user are as follows:

  1. Browser Console:
  • console.log(): Outputs information to the browser's console, which is useful for debugging and logging purposes.
  • console.info(): Outputs informational messages to the console.
  • console.warn(): Outputs warning messages to the console.
  • console.error(): Outputs error messages to the console.

2. Alert Boxes:

  • alert(): Displays a simple dialog box with a message and an OK button. It is primarily used for basic user notifications.

3. HTML Manipulation:

  • document.write(): Writes content directly into the HTML document. It is primarily used for testing or very specific scenarios.
  • DOM manipulation: Using JavaScript to modify the elements in the HTML document dynamically. This includes updating the text of HTML elements, adding or removing elements, and modifying attributes.

4. User Interface Manipulation:

  • innerHTML: Sets or retrieves the HTML content within an element. It allows you to update the content of an HTML element dynamically.

Example

document.getElementById("myElement").innerHTML = "Hello, World!";

5. Web APIs:

  • window.alert(): Similar to the alert() function, it displays a dialog box with a message and an OK button. This method is part of the window object.

Example:

window.alert("Hello, World!");
  • document.getElementById().textContent: Retrieves or sets the text content of an HTML element.

Example:

document.getElementById("myElement").textContent = "Hello, World!";
  • document.createTextNode(): Creates a new text node that can be appended to an HTML element.

Example:

var textNode = document.createTextNode("Hello, World!");
document.getElementById("myElement").appendChild(textNode);

These are the commonly used methods to display information in JavaScript, ranging from browser console output to manipulating the HTML document or user interface. The choice of method depends on the specific requirements and context of your JavaScript code. If you want to know more about javascript then choose the right place, Java Training Course in Lucknow, Indore, Meerut, Gwalior, Delhi, Noida and other cities of India. 

 

 

 

 


2

JavaScript can display data in different way.
1. innerHTML
2. window.document.write()
3. window.alert()
4. console.log()
5. window.print()
Inner HTML
     This property used in change the internal data of html elements.
</head>

<body>
    <p>JS demo</p>
    <p id='id1'></p>
    <script>
        document.getElementById('id1').innerHTML = 'this is body tag occur';
    </script>
</body>
</html>
document.write()
     It’s used for testing purpose, this is write data on the html page.
<!DOCTYPE html>

<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
window.alert()
     This is show an alert box on html page. You can skip the window keyword.
<html>

<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.alert(5 + 6);
</script>
</body>
</html>
console.log()
     This is used for debugging purpose. This data show on browser console.
<html>

<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
console.log(5 + 6);
</script>
</body>
</html>
window.print()
     This is used for print the current web page on paper.
<!DOCTYPE html>

<html>
<body>
<button onclick='window.print()'>Print this page</button>
</body>
</html>