EasyCode365EasyCode365

Your First JavaScript Code

If you have never written a single line of code before, you are in the perfect place. Today, we are going to learn how to make the computer "talk" to you by displaying a simple text message.

What JavaScript Output Means

When programmers write code, they often need a way to see what the computer is doing behind the scenes. We do this by creating an output, which simply means making the computer print a message onto a special screen area.

This special area is called the console. Think of the console as a private notepad for developers. Regular visitors to a website usually do not see it, but it is an essential tool for you to check if your code is working the way you want it to.

Writing Your First Line of Code

To send a message to the console, we use a built-in tool called console.log(). In this lesson, the output appears below the code after you click Run.

Here is your very first line of JavaScript code:

Can edit
console.log("Hello, JavaScript!");

Breaking Down the Code

Let's look at every single part of that instruction so you know exactly what is happening:

  • console.log: This is the command that tells the computer, "I want to send a message to the output area."
  • () (Parentheses): These act like a basket. They hold the exact value or message that we want to show on the screen.
  • "Hello, JavaScript!": The actual text we want to print is wrapped in quote marks. In programming, any normal text surrounded by quotes is called a string. You can use double quotes (" ") or single quotes (' '), but the text must be inside them!
  • ; (Semicolon): A semicolon often marks the end of an instruction. JavaScript can sometimes add it automatically, but using it here keeps the example clear.

Mini Task

Now it is your turn to write some code.

  1. Edit the code block above.
  2. Replace "Hello, JavaScript!" with your favorite animal.
  3. Click Run and check the output below the code.

Hint: Make sure to wrap your animal's name in quote marks so it becomes a string, and put it inside the parentheses!

Short Quiz

Question 1: What do we call a piece of text wrapped in quote marks in JavaScript? A) A console B) A string C) A parenthesis

Question 2: What is the purpose of the semicolon (;) at the end of our code? A) It marks the end of an instruction. B) It creates a new blank line. C) It prints the message to the screen.

(Answers: 1 - B, 2 - A)

Small Challenge

Are you ready to write multiple instructions?

For this challenge, write two separate lines of code.

  • On the first line, print your first name.
  • On the second line, print your favorite food.

Remember to use a semicolon at the end of each instruction!

Can edit
// put your code below

console.log()

Summary

Great job! You have officially written JavaScript. Here is a quick review of what you learned today:

  • You can use the console.log() command to send messages to the developer output area.
  • Text wrapped in quotes is called a string.
  • Parentheses hold the exact message you want to print.
  • A semicolon is used to mark the end of a coding instruction.