JavaScript For Kids For Dummies
Book image
Explore Book Buy On Amazon

It's time to start experimenting with some real JavaScript code! If you don't already have it open, open the JavaScript Console by selecting it from the Other Tools menu under the Chrome menu, or by clicking the Console tab in the Developer Tools.

Follow these steps to run your first JavaScript commands:

  1. Click inside the JavaScript console, near the >, to start inserting code.

  2. Type 1 + 1 and then press Return (Mac) or Enter (Windows).

    The browser gives you the answer on the next line.

Notice that when the answer is returned to you, it has an arrow on the left side of it that points to the left. This arrow indicates that the value came from JavaScript rather than from your input. Any value that comes from JavaScript is called a return value. Every command that you run in JavaScript produces some sort of return value.

Simple math is one thing, but JavaScript can do much, much more. Try out some other commands and see just how quickly you can get some answers around here.

Before you get started, clean up the console and remove any previous commands, errors, and return values in there. To clear the console, look at the upper-left corner and click the circle with the line through it. Everything inside the console will be erased, and now you've got a clean slate.

Click your mouse next to the > and try out the following JavaScript commands. Make sure to press Return (Mac) or Enter (Windows) after each one to see the results.

JavaScript Command Description
2000 – 37 This is a simple math problem, but this time you're using the minus sign to subtract the number on the right from the number on the left.
30 * 27 The asterisk (*) is how you tell JavaScript to multiply numbers.
120 / 20 The forward slash (/) tells JavaScript to divide the number on the left by the number on the right.
"Your name" + " " + "
is learning JavaScript!"
Yes, you can add words together with JavaScript! When you run a command that adds words together, it's called concatenation. The result will be that the words are combined into a single word.
Notice that the words in the JavaScript command are inside quotes. These quotes are very important.
Your name + + is learning JavaScript! When you don't use quotes, JavaScript doesn't like that one bit. It returns an error message containing the keyword SyntaxError. A syntax error means that you've written something that isn't valid JavaScript. Any time you see a syntax error, it means that you've goofed. Take a close look at your code and look for typos, missing punctuation, or missing quotes.

About This Article

This article is from the book:

About the book authors:

Chris Minnick and Eva Holland are experienced web developers, tech trainers, and coauthors of Coding with JavaScript For Dummies. Together they founded WatzThis?, a company focused on training and course development.

This article can be found in the category: