JavaScript Output
Output is the result a script shows while it runs. JavaScript shows results in the console or writes them into the page.
Print a value
Showing a result is the job of the console.log command. The console prints one line for this call.
console.log("Loading done");Print several values
One call can print several values at once. The console joins them with spaces.
console.log("Score:", 42, "points");Write into an element
This code writes a result into the demo paragraph. The next line reads the text back and logs it.
document.getElementById("demo").textContent = "Result shown";
console.log(document.getElementById("demo").textContent);Example
console.log("Report start");
console.log("Items:", 3);
document.getElementById("demo").textContent = "Report ready";
console.log(document.getElementById("demo").textContent);Key points
- console.log: the command that prints values to the console
- textContent: the text held inside an element
- getElementById: a command that finds one element by its id
- An alert is a small box that pauses the page until it closes.
- document.write can wipe a page that has already loaded.
In this example, the first line prints a heading to the console. The second line prints a label and a number on one line. The third line writes text into the demo paragraph. The last line reads that text back and logs it.
Try it Yourself
Edit the code, then press Run.
Runs in your browser โ the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.
Exercise
Which command prints a value to the console?