12 Ways to See Output on Screen in JavaScript
JavaScript offers numerous ways to display output to users, whether for debugging, interaction, or simply to show information. Here are twelve common methods to see output on the screen in JavaScript:
1. Console Output
The console.log()
method writes a message to the browser's console. It's a great tool for debugging and inspecting values.
console.log("Hello, World!");
2. Alert Box
The alert()
function displays a pop-up alert box with a specified message, requiring the user to acknowledge it before continuing.
alert("Hello, World!");
3. Document Write
The document.write()
method writes directly to the HTML document, replacing the entire content if called after the document has loaded.
document.write("Hello, World!");
4. Inner HTML
The innerHTML
property sets or returns the HTML content of an element.
document.getElementById("myElement").innerHTML = "Hello, World!";
5. Text Content
The textContent
property sets or returns the text content of an element, excluding any HTML tags.
document.getElementById("myElement").textContent = "Hello, World!";
6. Window Prompt
The prompt()
function displays a dialog box that prompts the user for input, returning the input value when the user clicks "OK".
let userInput = prompt("Enter something:"); console.log(userInput);
7. Custom Modal/Popup
Custom modals or popups can be created using HTML and CSS, with JavaScript to control their display.
// JavaScript document.getElementById("myModal").style.display = "block";
Originally published at https://www.rustcodeweb.com on August 2, 2024.