Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

JavaScript variables give values names, operators combine or compare those values, and functions package instructions you can run again. In this lesson, you’ll predict what each short example does, then check the result.

How do JavaScript variables, operators, and functions work?

They work together: a variable stores a value under a name, an operator creates an expression from values, and a function can accept values and return a result. For example, a function can calculate a total from named prices using arithmetic operators.

Declare variables with const, let, or var

JavaScript variables are declared using one of three keywords: let, const, or var (MDN: Grammar and types). A declaration introduces a binding; initialization gives it an initial value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
let score = 0;

Here, let declares the name score, and = 0 initializes it. You may declare a let without an initializer; its value is then undefined. A const declaration, by contrast, must have an initializer.

Choose between const and let

Use const when you do not intend to assign a different value to the binding. Use let when you expect to reassign it:

const course = "JavaScript
oops";

Predict: what happens if you try to change course to another string? The assignment fails because const prevents reassignment. For a changing value, use let:

let score = 0;
score = score + 1;
score += 1;
// score is now 2

+= adds the right-hand value to the current value and assigns the result back.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

const does not freeze object contents

A const binding cannot be pointed at a different object, but the object itself can still be changed:

const settings = {};
settings.theme = "dark"; // allowed

// settings = {}; // TypeError: reassignment is not allowed

Predict: does setting settings.theme work? Yes. It changes a property on the existing object; it does not reassign the settings binding.

Block scope and a common early-reference error

let and const are block-scoped: a name declared inside braces is available only inside that block. var is not block-scoped; it belongs to its containing function or, at the top level, the global scope.

if (true) {
  let message = "inside";
  var status = "also inside";
}

// message is unavailable here
// status is available here

Predict: which name can be read after the block? status, because var is not limited to the block. In contrast, reading a let or const name before its declaration is processed in its block throws a ReferenceError. A var declaration is hoisted and starts as undefined; its assignment still occurs where written. For beginner code, prefer const and let unless you have a specific reason to use var (MDN: Grammar and types).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use operators to calculate and compare values

An expression is code that evaluates to a value. Arithmetic operators include +, -, *, /, % (remainder), and ** (exponentiation). The assignment operator = stores a value; it does not test whether two values are equal.

Precedence determines the order of operations

Predict the value of result before reading the comment:

const result = 1 + 2 * 3; // 7

Multiplication happens before addition. Parentheses let you specify a different grouping:

const grouped = (1 + 2) * 3; // 9

Use parentheses when they make the intended order easier to see, even if precedence already determines it (MDN: Expressions and operators).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

+ adds numbers or joins strings

The plus operator adds numbers, but it also concatenates strings. Operations are evaluated from left to right, so the point where a string appears matters:

const first = "3" + 4 + 5; // "345"
const second = 3 + 4 + "5"; // "75"

Predict both results. In the first expression, the initial string turns the following additions into concatenation. In the second, 3 + 4 is calculated first, producing 7, which is then joined with the string "5".

Prefer strict equality for clear comparisons

Use === to compare without converting values to another type first, and !== for strict inequality. The operators == and != can coerce different types, sometimes producing surprising results:

123 == "123";  // true
123 === "123"; // false

The first comparison converts values to a compatible type; the second requires both value and type to match. Strict equality is a clear default for introductory examples, though JavaScript codebases may use loose equality for particular purposes (MDN: Expressions and operators).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Define a function, call it, and use its return value

A function is a reusable set of statements that performs a task or calculates a value. A function declaration names the function and lists its parameters—the names for inputs it can receive:

function double(number) {
  return number * 2;
}

const result = double(4); // 8

Predict result: the call passes 4 as the argument, which becomes the parameter number. The function multiplies it by two, and return supplies 8 to the caller. Writing the declaration defines the behavior; double(4) calls the function and runs it. A function that reaches its end without an explicit value returned produces undefined (MDN: Functions).

Function declarations can be called before their textual declaration because declarations are hoisted. A function expression assigned to a const does not share that behavior. You do not need this distinction for the example above, but it can explain why moving some function code changes whether a call works.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Try a mini-exercise that combines all three

Predict the output before checking it. The function uses a parameter, arithmetic, and a return value; const names store the inputs and final result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function totalWithTax(price, taxRate) {
  return price + price * taxRate;
}

const itemPrice = 20;
const taxRate = 0.1;
const total = totalWithTax(itemPrice, taxRate);

// total is 22

price * taxRate calculates the tax (2), and price + that amount produces 22. The example illustrates the calculation only; it does not specify a real tax rate.

Continue practicing

For a more in-depth treatment, Eloquent JavaScript, 4th edition is available to read online and as a paperback. Its chapters “Values, Types, and Operators” and “Functions” extend the concepts in this lesson. It is optional; the examples above are enough to practice the fundamentals.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.