JAVASCRIPT / LESSON 03

Combine values with operators and expressions

Calculate results, compare values, and recognize conversion surprises.

Content updated

By the end, you can…

  • An expression produces a value. Use === to compare without automatic type conversion.
  • Predict the example's result, change one part, and verify the behavior in your own script.
Before you begin

One idea at a time

An operator performs an operation on values called operands. In 4 + 2, + is the operator and 4 and 2 are its operands. The whole expression evaluates to 6. Multiplication runs before addition: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. This priority is called precedence. A statement can use an expression, for example assigning its result or logging it. Parentheses group calculations when you want their order to be obvious.

Numbers support addition, subtraction, multiplication, division, and remainder. The same + operator also joins strings. If a string participates in addition, JavaScript may join text instead of adding numbers. Prefer explicit conversion when input is supposed to be numeric. Comparison expressions produce booleans that conditions can use in the next lesson.

What to remember first

Remember first

An expression produces a value. Use === to compare without automatic type conversion.

Read the code before trying it

+ - * / %Calculate with numbers
% gives the remainder: 7 % 2 is 1. Division can produce a decimal, not just an integer.
= and +=Assign a value
total += 2 adds two and assigns the result back. Assignment is different from equality.
=== and !==Compare value and type
3 === "3" is false. !== asks whether they differ. Prefer these to == and != while learning.
> < >= <=Compare order
Use numeric values when comparing quantities so string ordering does not surprise you.
&& || !Combine or invert conditions
With booleans, && requires both and || accepts either. ! reverses the true-or-false interpretation. && stops at the first falsy operand, while || stops at the first truthy one; otherwise each returns its last operand. This is short-circuit evaluation: the skipped expression does not run.
Number(text)Convert deliberately
Number converts numeric text to a number; invalid numeric text produces NaN (Not-a-Number, a special numeric value indicating a failed numeric result). Empty text becomes zero, so check emptiness separately.

Walk through an example

javascript
const price = 10;
const quantity = 3;
console.log(price * quantity);
console.log(7 % 2);
console.log(3 === "3");
console.log(Number("3") === 3);

Multiplication produces 30, remainder produces 1, and strict equality rejects the mismatched types. Explicit conversion makes the final comparison true. It does not change the original string; Number returns a new value. Division such as 10 / 3 produces a repeating decimal approximation, not exactly 3.33.

See it happen: try a small change

Combine values with operators and expressions: try it

Predict the result, choose a version, then compare the code and output. Try the controls when the example has them. Each version starts fresh.

Code to notice

console.log(2 + 3);
console.log("2" + 3);
console.log(Number("2") + 3);

Live result

Lesson example
index.htmlPreview

What changed: The results are 5, the text 23, and 5 after explicit numeric conversion.

Look a little closer

Values also have truthiness when used as conditions. false, zero, an empty string, null, undefined, and NaN are common falsy values. A nonempty string such as false written in quotes is truthy. The || fallback therefore replaces zero too; the ?? operator only replaces null or undefined. Compare the fallback variant before choosing a default for a real count.

A template literal uses backticks instead of quotes. Inside it, ${expression} inserts a calculated value into text. For example, `Total: ${price * quantity}` creates a readable label. This is another way to combine text and values without a long chain of + operations; it does not update automatically when variables change later.

Try it in your own project

Use the existing linked script and Console. Run the numeric calculation, then compare a quoted quantity with a number. Replace the quantity with zero and check that the total is zero. Save and reload for each case.

The interactive preview runs in its own isolated document. Its Output panel mirrors console.log messages; it is not a full DevTools console. Choosing another version or resetting starts a fresh example and does not edit your local files. Read any simulation label: storage and request demonstrations explain their offline substitutes, while the written local examples use the real browser APIs.

A common wrong turn

Do not use = when you mean ===. Also avoid assuming a numeric-looking input is already a number. Check the value and type before calculating, and keep the meaningful number 0 when choosing a default. Parentheses improve clarity even when operator precedence would already give the intended result.

Your turn: combine values with operators and expressions

Calculate the cost of three notebooks and build a label with the numeric total.

Optional exploration

Try the related prediction challenge below, or run the task above in your own script. The challenge asks you to choose an output and explains the result.

Try Same value, different type →

  1. Store a price and a quantity as numbers and multiply them.
  2. Compare the quantity with numeric 3 and string "3" using ===. Predict true then false.
  3. Log a template-literal label and test a zero quantity. Your total should stay zero.

Go to the source

Examples are authored for Tuklas and checked with code and reference documentation. This is not a claim of independent human review. Suggest a correction.