Skip to main content

Archive

Show more

How To Print Hello World Using Javascript

Printing 'Hello World' with JavaScript

Displaying 'Hello World' is a basic exercise in JavaScript, and it can be done using several methods, each with its specific use case:


1. Using document.write()

The document.write() method writes text directly to the HTML document. While simple to use, it's generally not recommended for modern web development because it can overwrite the entire document if used after loading is complete.

document.write('Hello World!');

2. Using console.log()

The console.log() method outputs text to the browser’s console, which is useful for debugging. It does not affect the webpage’s content and is accessed via the browser’s developer tools.

console.log('Hello World!');

3. Using alert()

The alert() method shows a popup dialog box with the text. It’s useful for quick notifications but can be disruptive to users as it requires interaction to dismiss.

alert("Hello World!");

Conclusion

Each method for printing 'Hello World' in JavaScript has its own use case. document.write() is simple but can be problematic if used improperly. console.log() is best for debugging without affecting the page, and alert() provides immediate feedback but can be intrusive. Choosing the right method depends on your specific needs.

Comments