Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchiTechGuides 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:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →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.
#1 Best Overall
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.
const does not freeze object contents
A const binding cannot be pointed at a different object, but the object itself can still be changed:
Rank #2
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).
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →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).
+ 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:
Rank #4
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).
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteDefine 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:
Best Value
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.
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.
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.
Quick Recap
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.

