JavaScript can "display" data in different ways :-
- innerHTML using for Writing into an HTML element
- document.write()using for writing into an HTML output
- window.alert() using for writing into an alert box
- console.log() using for writing into the browser console
1) Using innerHTML
- JavaScript can use the document. getElementById (id) To access an HTML element
- The id attribute use to defines the HTML element.
- The HTML content is defined by the innerHTML property.
For example -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Tutorial</title>
</head>
<body>
<h4>Here we will write welcome to teknowize using innerHTML</h4>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "welcome to teknowize";
</script>
</body>
</html>
Output
2) Using document.write()
JavaScript can use the document.write() basically for testing purposes.
For example -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Tutorial</title>
</head>
<body>
<h4>Using document.write() for testing purpose</h4>
<script>
document.write(12+30);
</script>
</body>
</html>
Output
3) Using window.alert() or alert()
JavaScript can use the window.alert() or alert() to display data in a alert box.
For example -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Tutorial</title>
</head>
<body>
<h4>Using window.alert() to show data in alert box</h4>
<script>
alert(12+30);
</script>
</body>
</html>
Output
4) Using console.log()
JavaScript can use the console.log() basically for debugging purposes.
You can see output of console.log() by right click on mouse in web browser then go to console from right box.
For example -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Tutorial</title>
</head>
<body>
<h4>Using console.log() for debugging purpose</h4>
<script>
console.log(12+30);
</script>
</body>
</html>
Output