Hello, World! in JavaScript: A Short History and Your First Program
JavaScript is the language of the web: every major browser runs it, so it’s the one language you can start using without installing anything. Here’s where it came from, and how to write and run your first program.
A short history of JavaScript
In May 1995, Netscape, maker of the most popular web browser of the time, asked programmer Brendan Eich to create a simple scripting language that would let web pages react to the people using them. He wrote the first version in about ten days. It was first called Mocha, then LiveScript, and was renamed JavaScript in December 1995, shortly before it shipped in Netscape Navigator 2.0.
Despite the name, JavaScript has very little to do with Java. The name was largely a marketing decision: Java was the most talked-about language of 1995, and Netscape had a partnership with Java’s creator, Sun Microsystems.
Microsoft soon released its own version for Internet Explorer, and to keep the two compatible, the language was handed to the standards body Ecma International. The first official standard, called ECMAScript, was published in 1997. A major update in 2015 (often called ES6) added much of what modern JavaScript looks like today, and a new edition has been released every year since.
In 2009, Ryan Dahl released Node.js, which runs JavaScript outside the browser, on servers and on your own computer. That turned JavaScript from a browser-only language into one used for websites, servers, command-line tools and even desktop and mobile apps.
The code
console.log('Hello, World!');
That’s the whole program. console.log writes a message to the console, a text output area that every browser and Node.js provide. The text in quotes is a string; single quotes and double quotes both work in JavaScript.
Running it in your browser
No installation needed:
- Open any web page in Chrome, Edge, Firefox or Safari (a blank tab works).
- Open the developer tools console:
- Chrome or Edge: press Ctrl + Shift + J (Windows, Linux) or Cmd + Option + J (Mac).
- Firefox: press Ctrl + Shift + K (Windows, Linux) or Cmd + Option + K (Mac).
- Safari: first turn on Show features for web developers in Settings → Advanced, then press Cmd + Option + C.
- Type
console.log('Hello, World!');and press Enter.
You’ll see Hello, World! printed below your code. (Chrome may ask you to type “allow pasting” before it lets you paste code into the console, as a safety measure.)
Running it as a web page
To make a page that runs your program every time it loads, save this as hello.html:
<!doctype html>
<html>
<body>
<script>
console.log('Hello, World!');
document.body.textContent = 'Hello, World!';
</script>
</body>
</html>
Open the file in your browser (double-click it, or drag it onto a browser window). The second line inside <script> puts the message on the page itself, and the first still prints it to the console.
Running it with Node.js
-
Install Node.js from nodejs.org (the LTS version is the safe choice).
-
Open a terminal (Terminal on Mac, PowerShell or Command Prompt on Windows) and check it worked:
node --version -
Save the one-line program in a file called
hello.js. -
In the terminal, go to the folder where you saved it and run it:
cd path/to/your/folder node hello.js
The terminal prints:
Hello, World!