JavaScript relies on host objects for all its input and output needs. It's a bit hard to experiment with the native features without receiving or displaying anything, so here are examples of how to do basic output. These examples print "Hello, World!" on a single line on the screen, and nothing else.
Client-side JavaScript, in an HTML file to be loaded by a browser:
<HTML>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
document.write('Hello, World!\n');
</SCRIPT>
</BODY>
</HEAD>
Standalone JavaScript, in a plain text file to be supplied to an interpreter:
Clib.puts('Hello, World\n');
The functions document.write()
and Clib.puts()
are host functions (or host methods), not native features of the JavaScript language. All web browsers hosting JavaScript support document.write()
but since a standalone interpreter isn't a web browser, it has a different facility. The string 'Hello,
World!\n'
on the other hand, is a native JavaScript feature - a string constant. It's very common in JavaScript to see host and native features mixed up closely as in each of these examples. In these examples, the single, 'Hello,
World!\n'
line can be replaced with any number of lines of JavaScript.