Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

Day 9: Learning JavaScript Variables, Operators & Functions

Understand how JavaScript bindings, operators, and functions work through beginner-friendly examples, output predictions, and a practical mini-exercise.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

JavaScript variables give values names, operators combine or compare values, and functions package reusable behavior. Learn how they work by predicting each example before reading its result.

How do JavaScript variables, operators, and functions work?

Variables are named bindings: they let your code refer to a value. Operators build expressions that calculate or compare values, and functions group statements so you can run the same task with different inputs. MDN summarizes the declaration choices: “Variables in JavaScript are declared using one of three keywords: let, const, or var.” MDN’s declarations guide explains the distinction.

As an Amazon Associate I earn from qualifying purchases.

Declare variables with const and let

Declaration versus initialization

A declaration introduces a binding; initialization gives it an initial value. In let score = 0;, let declares the name and = 0 initializes it. A let binding can also be declared without an initializer, in which case its value is undefined until assigned. A const binding must be initialized when declared.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
let score;
console.log(score); // undefined
score = 0;

const player = "Mina";

Choose based on reassignment

Use const when you do not plan to assign a different value to the binding; use let when reassignment is part of the logic. const does not make an object immutable: it prevents replacing the binding, not changing the object’s properties.

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

// settings = {}; // TypeError: cannot reassign a const binding

let attempts = 0;
attempts = attempts + 1; // allowed

Predict the value of settings.theme after the assignment. It is "dark": the object changed, while the binding still points to the same object.

Block scope and the difference from var

A block is code enclosed in braces, such as an if body. Bindings declared with let or const inside a block are not available outside it. var is scoped to its containing function, or to the global scope when declared at the top level, rather than to the nearest block.

if (true) {
  let message = "inside";
  var count = 1;
}

// console.log(message); // ReferenceError: message is block-scoped
console.log(count); // 1

There is also a timing difference. A let or const binding cannot be used before its declaration has been processed in that block; trying to do so throws a ReferenceError. A var declaration is hoisted and starts as undefined, but its assignment stays where it appears.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
console.log(total); // undefined
var total = 5;

// console.log(rate); // ReferenceError
let rate = 2;

For beginner code, prefer const and let so the scope and reassignment behavior are explicit. MDN’s guide to variable scope has further detail.

Use operators to calculate and compare

Arithmetic, assignment, and precedence

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

let points = 10;
points += 3; // points is now 13

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

Predict both results before checking the comments. Multiplication takes precedence over addition, so result is 7. Parentheses make the addition happen first, so grouped is 9. Use parentheses when they make the intended grouping easier to read. See MDN’s expressions and operators guide.

+ can add or join strings

The plus operator adds when it is working with numbers, but it can concatenate (join) strings. Evaluation proceeds from left to right, so the point at which a string enters the expression matters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const first = "3" + 4 + 5; // "345"
const second = 3 + 4 + "5"; // "75"

In the first expression, the initial string causes the following values to be joined as text. In the second, 3 + 4 is calculated as 7 first; then that number is joined to the string "5". When you intend arithmetic, make sure the values are numbers.

Prefer strict comparisons in introductory examples

Use === to test equality without converting values of different types, and !== for strict inequality. This is a clear default while learning; it is not a claim that every JavaScript codebase must use the same style in every case.

console.log(123 == "123");  // true
console.log(123 === "123"); // false

The loose comparison == converts the string in this example before comparing. Strict equality keeps the number and string distinct, which avoids many surprises. MDN’s equality-operator reference describes the comparison operators.

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

Define a function, call it, and return a result

A function is a reusable set of statements that performs a task or calculates a value. A function declaration names the function and specifies its parameters and body. Declaring it describes what it will do; calling it runs the body.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function double(number) {
  return number * 2;
}

const result = double(4); // 8

Here, number is the parameter—the name the function uses for an input. The value 4 is the argument passed when calling double(4). The return statement supplies the calculated result to the caller, so result becomes 8. If a function finishes without an explicit return value, its result is undefined.

Function declarations can be called before their textual declaration because declarations are hoisted. A function expression assigned to a const does not work that way: the binding must be initialized before it is called. For a first example, keeping the declaration above the call makes the execution order easy to follow. Read more in MDN’s functions guide.

Mini-exercise: calculate a price total

Predict the output before reading the comment. This example uses a function to calculate a total, a parameter for its input, an operator to multiply, and a const binding for the result.

function priceWithTax(price, taxRate) {
  return price * (1 + taxRate);
}

const total = priceWithTax(20, 0.1);
console.log(total); // 22

The expression 1 + taxRate is 1.1; multiplying it by 20 gives 22. The function returns that value, and the call stores it in total. For optional deeper practice, the official site offers Eloquent JavaScript, 4th edition, including chapters on operators and functions; it is available to read online and as a paperback.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.