Hello World in JavaScript
Write a Hello World program in JavaScript with one console.log line. Simple explanation, how to run it in Node or the browser, and the exact output shown.
1// The simplest JavaScript program2console.log("Hello World");
Sample Output
Hello World
In this one
- How console.log prints a message
- What a string is
- How to run a JavaScript file with Node
- Where browser output appears in DevTools
How it works
- 1
The line starting with // is a comment. JavaScript ignores it.
- 2
console.log is called with one string, "Hello World".
- 3
The console prints the text without the quotes.
- 4
The program ends because there are no more lines.
Edge cases
Output not visible on the page
You ran the script in a browser and the page is blank.
console.log writes to DevTools, not the page. Press F12 and open Console. To show text on the page you would use document.body.textContent instead.
Missing quotes
console.log(Hello World) throws an error.
console.log(Hello World) is a SyntaxError: the words without quotes do not form a valid argument. Some engines report "missing ) after argument list". The code cannot run until its syntax is corrected. By contrast, console.log(Hello) is valid syntax but throws a ReferenceError if Hello is not defined. Put the message inside quotes.
Wrong capital letters
Console.log("Hello World") fails.
JavaScript is case-sensitive. The object is console with a small c.
What's going on
This program prints the words Hello World. It is the first program most people write in any language. The goal is not the message. The goal is to prove that your setup works.
The whole program is one line: console.log("Hello World"). There is no main function and no class. JavaScript runs the file from top to bottom, so this single line is enough.
console.log is a built-in function that writes text to the console. The console is a text window where programs show messages. The text inside the quotes is called a string. A string is a piece of text.
Where you see the output depends on where you run it. In Node, save the file as hello.js and type node hello.js in the terminal. In a browser, the text does not appear on the web page. It shows up in DevTools. Press F12 and open the Console tab. Almost every beginner looks at the page once and thinks nothing happened.
If Hello World prints, your editor, Node, or browser console is working. Now you can move on to real programs.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does console.log("Hello World") do?
Pick an answer to check it.
Question 2
Where do you see console.log output in a browser?
Pick an answer to check it.
Questions people ask
Do I need a semicolon at the end?
It is optional in most cases. JavaScript adds it for you. Many teams still write it to keep style the same.
Can I use single quotes?
Yes. 'Hello World' and "Hello World" print the same text. Pick one style and stay with it.
Why does the browser page stay empty?
console.log only writes to the developer console. Open DevTools with F12 to see the message.
How do I run this without installing anything?
Open any browser, press F12, click Console, paste the line and press Enter.