The Building Blocks

Every useful program starts with a few ordinary pieces: values, names, comparisons, decisions, and repetition. These pieces do not look impressive by themselves. A variable that stores a number is not an app. A loop that prints five lines is not a product. Still, these are the pieces that larger programs keep using. This part slows down on purpose. Beginners often want to rush into projects, but projects become frustrating when the basics are blurry. If you do not understand how a value changes, an `if` statement chooses a path, or a loop knows when to stop, every later chapter becomes harder than it needs to be. You will learn how JavaScript stores information with variables, how it represents different kinds of data, how expressions produce values, how operators compare and combine those values, how decisions choose between branches, and how loops repeat work. You will also start building a habit that matters for the rest of the book: predicting before running. Before you press Enter, pause and ask, "What do I think this will print?" Then run it and compare. That small habit turns examples into practice. By the end of this part, you should be able to write small programs that remember information, calculate with it, ask questions about it, and repeat a task. You will not know everything yet, but you will have the basic grammar of JavaScript.

Chapters

3

Values and Variables

Programs need information. Sometimes the information is a number, such as a score or price. Sometimes it is text, such as a name. Sometimes it is a yes-or-no fact, such as whether a user is logged in. In JavaScript, each piece of information is a value. A variable gives a value a name so you can use it later. These are small ideas, but they turn up in almost every JavaScript program you will write. You will learn how to create a value, store it in a variable, give that variable a clear name, change the value when you need to, and leave comments that explain your thinking. That includes the practical decisions too: when to reach for `let` versus `const`, how reassignment actually works, and which naming habits save you from confusing bugs later.

4

Data Types

In the last chapter, you learned that a value is a piece of information and a variable is a name that refers to a value. This chapter looks more closely at the values themselves. JavaScript values have types. A type describes what kind of value you are working with. `42` is a number. `"hello"` is a string. `true` is a boolean. `undefined` and `null` both represent missing or empty values, but they are not used in exactly the same way. Types matter because JavaScript behaves differently depending on the type of value it has. Adding two numbers is not the same as joining two strings. Checking an empty string is not the same as checking the number `0`. Many beginner bugs come from expecting one type and getting another. So this chapter is about telling the types apart: recognizing the ones you will use most, checking a value with `typeof`, converting between types on purpose, and sidestepping the surprises that automatic conversion tends to cause.

5

Operators and Expressions

So far, you have created values, stored them in variables, and learned the main beginner data types. Now you need to do work with those values. Operators are the symbols and keywords that combine, change, or compare values. Expressions are pieces of code that produce values. Together, operators and expressions are how programs calculate totals, compare ages, check permissions, build messages, and decide what is true. The tools here are the operators: arithmetic for numbers, comparison for testing values, logical operators for combining conditions, and assignment for storing results back into variables. You will also see why `===` is usually the safer choice over `==`, what `%` is good for, and how parentheses change the order things happen. The habit worth building is reading a long expression in small pieces instead of trying to take it in all at once.

6

Making Decisions

Programs become useful when they can choose between paths. So far, your code has mostly run from top to bottom. It creates values, stores them, calculates with them, compares them, and prints results. Real programs also need to ask questions. Is the user logged in? Is the cart empty? Is the score high enough to pass? Did the user type a name? JavaScript answers questions like these with conditions. A condition is an expression used to decide whether a block of code should run. This chapter covers the tools for answering them: `if`, `else`, and `else if` statements, truthy and falsy values, the ternary operator, the `switch` statement, and the habit of testing decisions with boundary values.

7

Loops

A loop repeats code. Without loops, printing the numbers from 1 to 100 would require 100 lines. With a loop, it takes a few lines. That is the obvious benefit. The deeper benefit is that loops let a program handle work when the amount of work can change. If a shopping cart has 3 items, you can loop over 3 items. If it has 300 items, the same loop can handle 300. If a name has 4 characters or 40 characters, a loop can walk through the string one character at a time. You will meet three loop shapes here: `while`, `do...while`, and `for`. Around them come the ideas that make loops reliable: counters, accumulators, `break` and `continue`, and nested loops, along with the off-by-one mistakes that catch everyone at least once. Most of the skill is choosing the right loop for the problem in front of you, and that part gets easier with practice.