Types and Statements
A JavaScript program is built from statements, and statements work with values of different types. A statement is an instruction that does something: declare a variable, make a decision, repeat a block. An expression is a piece of code that produces a value. Seeing the difference between the two, and knowing the basic value types, makes error messages and code structure much easier to understand.
Values and their types, briefly
Every value has a type. The everyday ones are numbers (7, 2.5), strings ('hello'), booleans (true, false), undefined, null, and objects, which include arrays and functions. JavaScript decides types while the program runs, so the same variable can hold a number now and a string later. The following lessons explore each type in detail; for now it is enough to know that a value's type decides what operations make sense on it.
Expressions produce values
Anything that results in a value is an expression: 3 + 4, userName, price * 1.18, isReady ? 'Go' : 'Wait' or a function call like Math.max(2, 9). Expressions can be nested inside one another and placed anywhere a value is expected, such as on the right side of = or inside a template literal.
The main kinds of statements
| Kind | Examples | Purpose |
|---|---|---|
| Declaration | let, const, function, class | Create variables, functions and classes. |
| Expression statement | count++;, save(); | An expression used on its own line for its effect. |
| Conditional | if...else, switch | Choose which code runs. |
| Loop | for, for...of, while, do...while | Repeat code. |
| Jump | return, break, continue, throw | Leave a function, loop or block early. |
| Error handling | try...catch...finally | Recover from errors. |
| Block | { ... } | Group statements and create a scope for let and const. |
Statements in a small program
const marks = [72, 45, 88, 30]; // declaration with an expression
let passed = 0; // declaration
for (const mark of marks) { // loop statement
if (mark < 0) { // conditional
throw new Error('Invalid mark'); // jump: throw
}
if (mark >= 40) {
passed++; // expression statement
} else {
continue; // jump: skip to the next mark
}
}
const label = passed === marks.length ? 'All passed' : `${passed} passed`;
console.log(label); // "3 passed"
switch (typeof label) {
case 'string':
console.log('label is text');
break; // without break, the next case runs too
default:
console.log('unexpected type');
}
Why the difference matters
Some places accept only expressions. Inside a template literal ${...}, as a function argument, or inside JSX braces, you cannot write an if statement; you need an expression form such as the ternary operator. Knowing that if is a statement and ? : is an expression tells you which to use.
Statements are usually ended with a semicolon. Block statements like if, for and function declarations end with a closing brace and do not need one.
A mistake with return and line breaks
JavaScript inserts semicolons automatically in some places, and one of them causes a famous bug. If you write return on its own line and the value on the next, JavaScript reads it as return; and the function returns undefined:
return followed by a newline and then { ok: true } returns nothing. Keep the value on the same line as return, or start the opening brace or bracket on that line. A related slip is leaving break out of a switch case, so execution falls through into the next case.
Main points
Expressions produce values; statements perform actions and give a program its structure. Use declarations, conditionals, loops and jump statements to control flow, and choose expression forms where only values are allowed. Keep returned values on the same line as return, and end each switch case with break.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.