Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Using window.alert()

You can employ an alert box for showcasing data.

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
window.alert(5 + 6);
</script>

</body>
</html>

In JavaScript, the window object is the global scope, so variables, properties, and methods automatically belong to it, making the window keyword optional.

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
alert(5 + 6);
</script>

</body>
</html>

Using console.log()

During debugging, you have the option to utilize the console.log() method in the browser to showcase data.

You’ll delve deeper into debugging in a subsequent chapter.

Example

<!DOCTYPE html>
<html>
<body>

<script>
console.log(5 + 6);
</script>

</body>
</html>

JavaScript Print

JavaScript doesn’t have built-in print objects or methods and can’t directly access output devices. However, in a browser, you can use window.print() to print the content of the current window.

Example

<!DOCTYPE html>
<html>
<body>

<button onclick=”window.print()”>Print this page</button>

</body>
</html>