JavaScript Output defines the ways to display the output of a given code. JavaScript can “display” data in different ways:
a) Writing into an HTML element, using innerHTML.
b) Writing into the HTML output using the document.write().
c) Writing into an alert box, using a window.alert().
d) Writing into the browser console, using console.log().
a) 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>
<h4>This is an HTML heading.</h4>
<p>This is a paragraph.</p>
<p id="js-1"></p>
<script>
document.getElementById("js-1").innerHTML = 5 + 6;
</script>
</body>
</html>
Output:
This is an HTML heading.
This is a paragraph.
Changing the innerHTML property of an HTML element is a common way to display data in HTML.
b) Using document.write()
For testing purposes, it is convenient to use the document.write():
Example:
<!DOCTYPE html>
<html>
<body>
<h4>This is an HTML heading</h4>
<p>This is a paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Output:
This is an HTML heading
This is a paragraph.
11
Note: The document.write() method should only be used for testing.
c) Using window.alert()
You can use an alert box to display data:
<!DOCTYPE html>
<html>
<body>
<h4>This is an HTML heading </h4>
<p>This is a paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Output:
d) Using console.log()
For debugging purposes, you can use the console.log() method to display data:
<!DOCTYPE html>
<html>
<body>
<h4>Press F12</h4>
<p>Select "Console" in the debugger menu. Then click Run again.</p>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JavaScript is the programming language of HTML and the Web. Java Script is one of the 3 languages all web developers should learn:
a) HTML – to define the content of web pages. b) CSS – to specify the layout of web pages. c) JavaScript – to program the behaviour of web pages.
Web pages are not the only places where JavaScript is used. Many desktop and server programs use JavaScript (for example Node.js).
JavaScript can change the HTML content. One of many JavaScript methods is getElementById(). This example uses the method to “find” an HTML element (with id=”demo”) and changes the element content (innerHTML) to “Hello JavaScript”:
<!DOCTYPE html>
<html>
<body>
<h4>What Can JavaScript Do?</h4>
<p id="exampleOne">JavaScript can change HTML content.</p>
<button type="button" onclick='document.getElementById("exampleOne").innerHTML = "Hello JavaScript!"'>Click Me!</button>
</body>
</html>
Output:
What Can JavaScript Do?
JavaScript can change HTML content.
JavaScript can change HTML attribute values. In this example, JavaScript changes the value of the src (source) attribute of an <img> tag:
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('image').src='pic_bulbon.gif'">Turn on the light</button>
<img id="image" src="pic_bulboff.gif" style="width:100px">
<button onclick="document.getElementById('image').src='pic_bulboff.gif'">Turn off the light</button>
</body>
</html>
Output:
JavaScript can change HTML styles (CSS). Changing the style of an HTML element is a variant of changing an HTML attribute:
<!DOCTYPE html>
<html>
<body>
<h4>What Can JavaScript Do?</h4>
<p id="examle">JavaScript can change the style of an HTML element.</p>
<button type="button" onclick="document.getElementById('example').style.fontSize='35px'">Click Me!</button>
</body>
</html>
Output:
What Can JavaScript Do?
JavaScript can change the style of an HTML element.
JavaScript is the programming language for the web and can update and change both HTML and CSS.
You can place any number of scripts in your HTML document.
You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML, in the <body> section, or after the </body> close tag, depending on when you want Java Script to load.
In this example, a JavaScript function is placed in <head> section of HTML page. The function is invoked (called) when a button is clicked:
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("example").innerHTML = "Text changed.";
}
</script>
</head>
<body>
<h4>JavaScript in the Head Section</h4>
<p id="example">Click on the button to change the text.</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>