Skip to content

JavaScript Output Methods

JavaScript provides several ways to produce output, ranging from simple message displays to more complex manipulations of the Document Object Model (DOM).

1. console.log()

Used for debugging, this method outputs information to the browser’s console.

console.log(‘Hello, World!’);

2. alert()

Displays an alert dialog with a specified message and an OK button.

alert(‘This is an alert box!’);

3. document.write()

Writes directly to the HTML document. It’s rarely used in modern web development due to potential issues with overwriting existing content.

document.write(‘This is written directly to the document.’);

4. innerHTML

Allows you to change the HTML content of an element.

document.getElementById(‘myElement’).innerHTML = ‘New Content’;

5. innerText and textContent

Both properties set or return the text content of the specified node.

document.getElementById(‘myElement’).innerText = ‘New Text’; document.getElementById(‘myElement’).textContent = ‘New Text’;