Therefore, let's discuss each and every one of them.
When access an HTML element, JavaScript can use the document.getElementById(id) method.
The HTML element is defined by its id attribute. The innerHTML property specifies the HTML content:
<!DOCTYPE>
<html>
<body>
<h1>
</h1>
<p id="demo">
</p>
<script>
</script>
</body>
</html>
In the above example, we check the HTML element with "id=demo" and change its content to 11.
Note: Changing an HTML element's innerHTML property is a common way to display data in HTML.
An alert box can be used to display data as shown in example below:
<!DOCTYPE>
<html>
<body>
<h1>
</h1>
<p id="demo">
</p>
<script>
</script>
</body>
</html>
In JavaScript, the window object is the global scope object, that means that variables, properties, and methods by default belong to the window object. This also implies that specifying the window keyword is optional
You can omit the window keyword:
<!DOCTYPE>
<html>
<body>
<h1>
</h1>
<p id="demo">
</p>
<script>
</script>
</body>
</html>
It is more convenient to use a document.write() for testing purposes:
<!DOCTYPE>
<html>
<body>
<h1>
JavaScript Example for document.write()</h1>
<p id="demo">
</p>
<script>
</script>
</body>
</html>
The document.write() method should only be used for testing.
Note: Using document.write() after an HTML document is loaded, will delete all existing HTML
To display data for debugging purposes, use the console.log() method in the browser.
<!DOCTYPE>
<html>
<body>
<h1>
</h1>
<p id="demo">
</p>
<script>
</script>
</body>
</html>
There are no print objects or methods in JavaScript.
JavaScript does not have access to output devices.
The only exception is that you can call the window.print() method in the browser to print the content of the current window.
Next