Skip to content
Advertisement

How to write a simple hello world program in Javascript?

I’ve been looking all over the world.

All I see is samples alerting hello world

I don’t want to alert hello world.

I want to print a simple website saying hello world.

<!DOCTYPE html>
<html lang="en">
<head></head>
<body id="home">
    <script>
        //print hello world
    </script>
</body>
</html>

Does javascript have a print command?

Here are typical samples on the web

http://groups.engin.umd.umich.edu/CIS/course.des/cis400/javascript/hellow.html

Advertisement

Answer

In JS code you don’t ‘print’ to the screen. Instead you amend the properties of the HTML elements in the DOM.

To do what you require you can retrieve the #home element then set its text. Either of the below will work for you:

// POJS
document.getElementById('home').textContent = 'hello world';

// jQuery
$(function() {  
    $('#home').text('hello world'); 
});

<!DOCTYPE html>
<html lang="en">
<head></head>
<body id="home">
    <script>
        document.getElementById('home').textContent = 'hello world';
    </script>
</body>
</html>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement