---
title: "JavaScript – outputs"
url: https://khanwaseem.com/blog/course/javascript-outputs
author: "khan Waseem"
published: 2023-09-23
updated: 2023-09-23
topics: ["course", "Javascript Tutorial"]
description: "The JavaScript language’s fundamental concepts, syntax, and practical applications. Learn about variables, functions, loops, and more for web development."
---

# JavaScript – outputs

- Writing into an HTML element, using `innerHTML`.

- Writing into the HTML output using `document.write()`.

- Writing into an alert box, using `window.alert()`.

- Writing into the browser console, using `console.log()`.

Table of Contents

[Toggle](#)

- [1. .innerHTML:](#1-innerhtml)
- [2. document.write():](#2-documentwrite)
- [3. window.alert():](#3-windowalert)
- [4. console.log():](#4-consolelog)
- [5. windows.print() :](#5-windowsprint)

## 1. .innerHTML :

- `.innerHTML` is a property in JavaScript that allows you to change the content of an HTML element.

- You can use it to update the text or HTML markup within an element. **Example**:
Let’s say you have an HTML `<div>` element with the id “myElement”:

```xml
   <div id="myElement">This is some initial content.</div>Code language: HTML, XML (xml)
```

In JavaScript, you can change the content of this element like this:

```javascript
   let element = document.getElementById("myElement");
   element.innerHTML = "This is the new content.";Code language: JavaScript (javascript)
```

**Output : **

![](https://blogkhanwaseem.s3.amazonaws.com/uploads/2023/09/23105420/image-234.png)

After running this JavaScript code, the content of the `<div>` element will be replaced with “This is the new content.”

## 2. document.write() :

- `document.write()` is a JavaScript method that can be used to write content directly to the HTML document.

- It’s often used for testing or in scenarios where you want to add content to the page as it loads. **Example**:
You can use `document.write()` like this:

```javascript
   document.write("This content will be added to the document.");Code language: JavaScript (javascript)
```

**Output : **

![](https://blogkhanwaseem.s3.amazonaws.com/uploads/2023/09/23105723/image-235.png)

When the page loads, the text “This content will be added to the document.” will be written directly to the page where the JavaScript code is placed.

## 3. window.alert() :

- `window.alert()` is a JavaScript function that displays a pop-up dialog box with a message to the user.

- It’s commonly used to show important messages or notifications to the user. **Example**:
You can use `window.alert()` like this:

```javascript
   window.alert("Hello, this is an alert!");Code language: JavaScript (javascript)
```

**Output : **

![](https://blogkhanwaseem.s3.amazonaws.com/uploads/2023/09/23105822/image-236.png)

When this JavaScript code is executed, a pop-up alert with the message “Hello, this is an alert!” will appear on the user’s screen, and they need to click “OK” to dismiss it.

## 4. console.log() :

- `console.log()` is a JavaScript function used for debugging and logging information to the browser’s developer console.

- It’s helpful for developers to see what’s happening in their code without affecting the user interface. **Example**:
You can use `console.log()` like this:

```javascript
   let message = "This is a log message.";
   console.log(message);Code language: JavaScript (javascript)
```

**Output : **

![](https://blogkhanwaseem.s3.amazonaws.com/uploads/2023/09/23105858/image-237.png)

When you open the browser’s developer console (usually by pressing F12 or right-clicking and selecting “Inspect”), you’ll see the message “This is a log message.” logged in the console. It’s a useful way to check the values of variables or track the flow of your code.

## 5. windows.print() :

In JavaScript, `**window.print()**` is a method that allows you to trigger the printing of the current web page when a user clicks on a button or interacts with your web application. It’s a handy feature for providing a print-friendly version of your content. Here’s an explanation along with an example:

**Explanation**:

- `window.print()` is a function that, when called, opens the browser’s print dialog box.

- The print dialog allows the user to choose a printer and configure printing options.

- After the user makes their selections and clicks “Print,” the browser sends the content of the web page to the chosen printer.

**Example**:
Suppose you have a web page with a button, and you want to allow users to print the content of that page when they click the button. You can use `window.print()` for this purpose.

Here’s the HTML and JavaScript code for such a scenario:

```xml
<!DOCTYPE html>
<html>
<head>
    <title>Print Page Example</title>
</head>
<body>
    <h1>Welcome to My Web Page</h1>
    <p>This is some content that you can print.</p>

    <button onclick="printPage()">Print this Page</button>

    <script>
        function printPage() {
            // Call window.print() to open the print dialog
            window.print();
        }
    </script>
</body>
</html>Code language: HTML, XML (xml)
```

**Output **:

![](https://blogkhanwaseem.s3.amazonaws.com/uploads/2023/09/23110851/image-238.png)

**In this example:**

- We have an HTML button with the text “Print this Page.”

- When the button is clicked, the `printPage()` function is called, which contains the `window.print()` command.

- Clicking the button will open the browser’s print dialog, allowing the user to select a printer and print the content of the web page.

Using `window.print()` in this way makes it easy for users to generate a printed version of your web page, making it more accessible for offline reading or documentation purposes.

These JavaScript features are essential for manipulating content on web pages, providing feedback to users, and debugging your code during development.

### Related posts:

[CSS – Table](https://blog.khanwaseem.com/css-table/)
[HTML – Tables](https://blog.khanwaseem.com/html-tables/)
[HTML – Attributes](https://blog.khanwaseem.com/html-attributes/)
[CSS – Paged Media](https://blog.khanwaseem.com/css-paged-media/)

Powered by [YARPP](https://yarpp.com).
