When we want to give any message to the user or print anything on the screen as output in JavaScript then following methods are used for that-
window.alert() Output Method
Example-
<!DOCTYPE html>
<html>
<head>
<title>JS OUTPUT METHOD</title>
</head>
<body>
<button onclick="demo()">Click ME</button>
<script>
function demo()
{
window.alert("Welcome dear user");
}
</script>
</body>
</html>
document.write() Output Method
It replaces all the document to print its result.
Example-
<!DOCTYPE html>
<html>
<head>
<title>JS OUTPUT METHOD</title>
</head>
<body>
<button onclick="demo()">Click ME</button>
<script>
function demo()
{
document.write("WELCOME to Jaischool.com");
}
</script>
</body>
</html>
console.log() Output Method
A JavaScript object can have properties and methods. So if you want to know that simply you can print/test objects in the console. Press Ctrl+Shift+i to go to the developer tool and then click on the console.
Example-
<!DOCTYPE html>
<html>
<head>
<title>JS OUTPUT METHOD</title>
</head>
<body>
<button onclick="demo()">Click</button>
<script>
function demo()
{
console.log(window.navigator);
}
</script>
</body>
</html>
innerHTML Output Property
With this output property (You can also say output method), you can write something in HTML element.
Example-
<!DOCTYPE html>
<html>
<head>
<title>JS OUTPUT METHOD</title>
</head>
<body>
<button onclick="demo()">write</button>
<div style="width:200px;height:50px;border:1px solid red;" id="result"></div>
<script>
function demo()
{
var div_ele = document.getElementById("result");
div_ele.innerHTML = "Javascript is a client side scripting language.";
}
</script>
</body>
</html>
Publish A Comment