JavaScript Output
JavaScript Output
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
JavaScript Output
❮ Previous Next ❯
Using innerHTML
To access an HTML element, JavaScript can use the document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines the HTML
content:
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
Changing the innerHTML property of an HTML element is a common way to display data in HTML.
Using document.write()
For testing purposes, it is convenient to use document.write() :
Example
<!DOCTYPE html>
<html>
<body>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Using document.write() after an HTML document is loaded, will delete all existing HTML:
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
ADVERTISEMENT
Using window.alert()
You can use an alert box to display data:
Tutorials
Example Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
<!DOCTYPE html>
<html>
<body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Try it Yourself »
In JavaScript, the window object is the global scope object. This means that variables, properties,
and methods by default belong to the window object. This also means that specifying the window
keyword is optional:
Example
<!DOCTYPE html>
<html>
<body>
<script>
alert(5 + 6);
</script>
</body>
</html>
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Try it Yourself »
Using console.log()
For debugging purposes, you can call the console.log() method in the browser to display data.
Example
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
Try it Yourself »
JavaScript Print
JavaScript does not have any print object or print methods.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
?
Exercise
What is NOT a correct syntax for writing output in JavaScript?
window.alert()
console.log()
body.html()
document.write()
Submit Answer »
❮ Previous Next ❯