Chapter 02
JavaScript Output
JavaScript can display information using the browser console, alert boxes, HTML elements, and other output techniques.
01 • Learn
What is JavaScript Output?
- console.log() displays output in the browser Developer Console.
- alert() displays a popup message to the user.
- innerHTML can change the content of an HTML element.
- document.write() can write content to a document, but it should be used carefully in modern applications.
02 • Syntax
Syntax
console.log('Hello');
alert('Welcome');
document.getElementById('demo').innerHTML = 'Hello';
03 • Example
Example
Try understanding this code before running it.
<!DOCTYPE html>
<html>
<body>
<h2 id="demo">Old Text</h2>
<button onclick="showOutput()">Show Output</button>
<script>
function showOutput() {
console.log('Hello from AIFA');
document.getElementById('demo').innerHTML = 'Hello from JavaScript';
}
</script>
</body>
</html>
04 • Remember
Important Points
- console.log() is very useful for debugging.
- innerHTML can change the content of an HTML element.
- alert() is useful for simple popup messages.
- Choose an output method according to the requirement of your application.
05 • Avoid
Common Mistakes
- Thinking that console.log() displays text directly on the webpage.
- Using the wrong HTML element ID.
- Using document.write() carelessly after the page has loaded.
06 • Practical
Real-World Use
Developers use console.log() for debugging and DOM methods for updating website content.
07 • Practice
Practice Exercise
Practice: JavaScript Output
Create a heading and a button. When the button is clicked, change the heading text using JavaScript.
Ready to write code?
Open the JavaScript editor and run your code with live output.