KodingKaro
JavaScript · Beginner · Syntax and Variables

Statements, expressions, and clear names

JavaScript source is built from statements that perform actions and expressions that produce values. Comments and clear names make the code easier to revisit.

✓ What you will learn in this topic

  • Distinguish a statement from an expression.
  • Write line and block comments.
  • Use valid descriptive identifiers.

Concept explanation

An expression produces a value, such as price * quantity. A statement performs an action, such as declaring a variable or choosing a branch. Semicolons separate statements; automatic semicolon insertion exists, but consistent explicit semicolons help avoid ambiguous line breaks. Comments explain intent, not obvious syntax.

Readable source helps teammates and your future self understand what a value means instead of reverse-engineering short, cryptic names.

Syntax

Identifiers may contain letters, digits, underscores, and dollar signs, but cannot begin with a digit or use reserved keywords. JavaScript names are case-sensitive.

Example 1: a small experiment

The multiplication is an expression that produces a number; each const declaration is a statement.

const unitPrice = 12;
const quantity = 3;
const subtotal = unitPrice * quantity;
console.log(subtotal);

Expected output: 36

Why it is useful: Named intermediate values make a calculation easier to read and update.

Example 2: a practical feature

Descriptive names and a template literal make the cart calculation and its displayed result understandable.

const cartItems = [
  { name: 'Notebook', price: 4.5, quantity: 2 },
  { name: 'Pen set', price: 6, quantity: 1 },
];
const cartTotal = cartItems.reduce((sum, item) => sum + item.price * item.quantity, 0);
console.log('Cart total: $' + cartTotal.toFixed(2));

Expected output: Cart total: $15.00

Why it is useful: Clear identifiers reduce mistakes when business calculations evolve.

Common mistakes

Do not redeclare a let or const in the same scope, and do not expect const to freeze an object. const prevents rebinding; object properties may still change.

Best practices

Prefer const unless a binding must be reassigned, use let for that case, and avoid var in new code. Choose descriptive camelCase names.

Practice exercise

Calculate the total for three named products. Use descriptive identifiers and add a comment only where it explains a decision that is not obvious from the code.

Lesson summary

Expressions produce values, statements act on them, and meaningful identifiers improve maintainability.

Knowledge check

Which part of `cost * count` is an expression?

Show explanation

The multiplication expression evaluates to a value that can be used or assigned.

Practical example

javascript
const cartItems = [
  { name: 'Notebook', price: 4.5, quantity: 2 },
  { name: 'Pen set', price: 6, quantity: 1 },
];
const cartTotal = cartItems.reduce((sum, item) => sum + item.price * item.quantity, 0);
console.log('Cart total: $' + cartTotal.toFixed(2));

Expected result:

Cart total: $15.00

Hands-on practice task

Calculate the total for three named products. Use descriptive identifiers and add a comment only where it explains a decision that is not obvious from the code.

In this section

Other lessons in Syntax and Variables

Broaden your knowledge

Related learning topics