Back to JavaScript
2026-01-319 min read

Hello World in JavaScript

Learn Hello World in JavaScript step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on writing "Hello, World!" in JavaScript! This simple program is a fundamental stepping stone for anyone learning to code and serves as an introduction to the language's syntax and structure. In this lesson, we will delve into why it matters, explore prerequisites, unravel the core concept, provide a worked example, discuss common mistakes, present practice questions, and answer frequently asked questions.

The Importance of "Hello, World!"

"Hello, World!" is often referred to as the first program that most beginners write when learning to code. It serves as an easy introduction to programming concepts such as variables, functions, and output. Understanding this simple program will provide a strong foundation for more complex JavaScript projects in the future. Additionally, it's often used during job interviews to test a candidate's basic understanding of a programming language.

The Role of "Hello, World!" in Learning Programming

The "Hello, World!" program is essential for several reasons:

  1. Simplicity: It consists of only a few lines of code, making it easy for beginners to grasp the basics of the language.
  2. Immediate Feedback: When executed correctly, the program produces an output that verifies its success, reinforcing the learner's understanding of the concepts involved.
  3. Familiarity: The "Hello, World!" program is a common starting point for many programming languages, making it easier to transition between different languages as needed.
  4. Building Block: The concepts learned while writing "Hello, World!" can be applied to more complex programs and projects in the future.

Prerequisites

To follow along with this lesson, you should have a basic understanding of:

  • Basic computer concepts (e.g., files, directories)
  • Text editors (e.g., Notepad, Visual Studio Code)
  • Web browsers (e.g., Google Chrome, Mozilla Firefox)
  • Familiarity with the command line or terminal (optional but useful for running JavaScript outside of a web browser)

Understanding Basic Computer Concepts

Before diving into writing "Hello, World!" in JavaScript, it's important to have a basic understanding of some fundamental computer concepts:

  1. Files: A collection of data stored on a computer's storage device. In the context of this lesson, we will be working with files containing JavaScript code.
  2. Directories (or folders): A container for organizing files and other directories on a computer's storage device.
  3. Paths: The location or address of a file or directory on a computer's storage device.
  4. Extensions: Additional characters added to the end of a file name that indicate its format or purpose (e.g., .js for JavaScript files).

Core Concept

Writing "Hello, World!" in JavaScript

To write "Hello, World!" in JavaScript, you'll need to create a script that includes the following steps:

  1. Define a variable to store the message.
  2. Use a function to display the message.
  3. Execute the function to output the message.

Here's an example of what this might look like:

// Step 1: Define a variable for the message
let greeting = "Hello, World!";

// Step 2: Use a function to display the message
function printGreeting() {
console.log(greeting);
}

// Step 3: Execute the function to output the message
printGreeting();

In this example, we first define a variable greeting and assign it the string "Hello, World!". We then create a function called printGreeting, which uses the built-in console.log() method to print the value of the greeting variable to the console. Finally, we call the printGreeting function to execute the program and output "Hello, World!"

Understanding Variables in JavaScript

Variables are used to store data in a program. In JavaScript, variables can be declared using the let, const, or var keywords. Here's an example of how to declare a variable:

let greeting = "Hello, World!";

In this example, we use the let keyword to declare a new variable called greeting. We then assign it the string "Hello, World!". Once declared, the value of the variable can be changed throughout the program.

Understanding Functions in JavaScript

Functions are reusable blocks of code that perform specific tasks within a program. In JavaScript, functions can be defined using the function keyword. Here's an example of how to define a function:

function printGreeting() {
console.log(greeting);
}

In this example, we use the function keyword to define a new function called printGreeting. The function takes no arguments and contains one line of code that prints the value of the greeting variable to the console using the built-in console.log() method.

Running JavaScript Code

There are several ways to run JavaScript code:

  1. In a web browser's developer console (press F12 to open it)
  2. By saving the script in a file with a .js extension and running it from the command line (e.g., using Node.js)
  3. By embedding the script within an HTML file and opening the HTML file in a web browser

Worked Example

Let's create a simple "Hello, World!" program that takes user input for their name and greets them personally:

// Ask the user for their name
let name = prompt("What is your name?");

// Define a personalized greeting message
let greeting = `Hello, ${name}!`;

// Use a function to display the personalized greeting
function printGreeting() {
console.log(greeting);
}

// Execute the function to output the personalized greeting
printGreeting();

In this example, we use the prompt function to ask the user for their name and store it in a variable called name. We then create a personalized greeting message by concatenating the value of the name variable with the string "Hello, ". Finally, we define a function called printGreeting, which uses the built-in console.log() method to print the personalized greeting message to the console.

Common Mistakes

1. Forgetting semicolons (;)

In JavaScript, semicolons are used to separate statements. If you forget a semicolon, it can cause syntax errors.

Correct:

let greeting = "Hello, World!";
console.log(greeting);

Incorrect (syntax error):

let greeting "Hello, World!"; console.log(greeting);

2. Misplaced or missing parentheses ()

Parentheses are crucial for defining functions and calling them correctly. If you forget to include them, your code will not work as expected.

Correct:

function printGreeting() {
console.log(greeting);
}
printGreeting();

Incorrect (syntax error):

function printGreeting printGreeting();

3. Incorrect variable assignment

Ensure that you are assigning values to variables using the = operator, not comparison operators like ==.

Correct:

let greeting = "Hello, World!";

Incorrect (syntax error):

let greeting == "Hello, World!";

4. Uninitialized Variables

If you try to use a variable that has not been declared or initialized, JavaScript will throw an error. To avoid this, make sure to declare and initialize your variables before using them in your code.

Correct:

let greeting;
greeting = "Hello, World!";
console.log(greeting);

Incorrect (syntax error):

console.log(greeting);
let greeting;

Practice Questions

  1. Write a program that calculates the sum of two numbers entered by the user using JavaScript.
  2. Create a program that converts Celsius to Fahrenheit and vice versa.
  3. Write a program that generates a random number between 1 and 100.
  4. Create a simple guessing game where the computer randomly selects a number between 1 and 10, and the user has to guess it within three attempts.
  5. Write a program that takes user input for their name and age, then prints out a personalized greeting with both pieces of information.
  6. Write a program that asks the user for two numbers and calculates their product using JavaScript.
  7. Write a program that checks if a given year is a leap year or not.
  8. Write a program that sorts an array of numbers in ascending order using JavaScript.
  9. Write a program that takes user input for a word, then prints out all the palindromes (words that read the same backward as forward) up to 5 letters long in the English language.
  10. Write a program that generates a random password with uppercase and lowercase letters, numbers, and special characters.

FAQ

Q: What is the purpose of the console.log() function in JavaScript?

A: The console.log() function is used to print output to the browser's developer console. It's a useful tool for debugging and testing your code.

Q: Can I run JavaScript outside of a web browser?

A: Yes, you can run JavaScript using Node.js, which allows you to execute JavaScript on your computer without a web browser.

Q: What are some other ways to output text in JavaScript besides console.log()?

A: Some alternative methods for outputting text in JavaScript include the alert() function (which displays a dialog box with the message) and the document.write() method (which writes text to an HTML document). However, these methods are typically used less frequently than console.log().

Q: How do I create a new file in my code editor to write JavaScript code?

A: To create a new file in your code editor, navigate to the "File" menu and select "New File" or press Ctrl+N (or Command+N on a Mac). You can then save the file with a .js extension to indicate that it contains JavaScript code.

Q: How do I run JavaScript code in my web browser's developer console?

A: To run JavaScript code in your web browser's developer console, open the console by pressing F12 or right-clicking on the page and selecting "Inspect". You can then copy and paste your JavaScript code into the console and press Enter to execute it.

Q: How do I run JavaScript code using Node.js?

A: To run JavaScript code using Node.js, you'll need to have Node.js installed on your computer. Once installed, open a terminal or command prompt, navigate to the directory containing your JavaScript file, and type node filename.js, replacing "filename" with the name of your JavaScript file. This will execute the JavaScript code in the file.

Q: What is the difference between let, const, and var in JavaScript?

A: In JavaScript, let and const are block scoped variables (meaning they are only accessible within the block or function where they are declared), while var is function scoped. Additionally, const variables cannot be reassigned once they have been initialized, while let and var can.

Q: What is a JavaScript object?

A: A JavaScript object is a collection of key-value pairs that allows you to store and manipulate data in a more organized manner. You can create an object using curly braces {}, and access its properties using dot notation (e.g., myObject.propertyName).

Q: What are some common JavaScript libraries or frameworks?

A: Some popular JavaScript libraries and frameworks include jQuery, React, AngularJS, Vue.js, and Express.js. These tools can help simplify the development process by providing pre-written code for common tasks and patterns.

Q: What is asynchronous programming in JavaScript?

A: Asynchronous programming allows JavaScript to perform multiple tasks concurrently without blocking the main thread. This is crucial for building responsive web applications, as it ensures that the user interface remains smooth and interactive even when complex calculations or network requests are being performed in the background. Common techniques for implementing asynchronous programming in JavaScript include callbacks, promises, and async/await.

Hello World in JavaScript | JavaScript | XQA Learn