JAVASCRIPT / CHAPTER 01
Your first JavaScript program
BeginnerName a value, print it, and follow a small program one statement at a time.
By the end of this chapter
- Run a small program in a browser console
- Create and update a named value
- Distinguish text, numbers, and booleans
The idea
A program is a sequence of instructions. Open the developer console in a browser, enter a statement, and press Enter to run it. console.log(...) prints a value. Text goes inside matching quotes; a number such as 2 is written without quotes. A semicolon can mark the end of a statement. The console may also display a return value such as undefined; that is separate from the line printed by console.log.
A variable gives a value a name. Write let completed = 2 to create a value you plan to change, then completed = completed + 1 to store its next value. The equals sign here means assignment: evaluate the right side, then save that result under the name on the left. Use const for a name you will not reassign. Identifier spelling and capitalization must match each time.
Read statements from top to bottom and track each name on paper. Numbers can be added; quoted text remains text. A boolean is either true or false and can describe a condition such as whether a target was reached. The next chapter develops comparisons and missing values. First predict the three output lines below, then run the program and compare.
const title = "Practice";
let completed = 2;
console.log(title);
completed = completed + 1;
console.log(completed);
console.log(completed >= 3);Practice 3 true
The title is text. The assignment changes completed from two to three, and the comparison is true.
Try it yourself
Create a project name and a task count. Increase the count by two, then print the name and the updated count on separate lines.
Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.
Open JavaScript coding challengesCHECK YOUR UNDERSTANDING