October 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 PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

JavaScript Variables and Functions for Beginners

Understand JavaScript bindings with const, let, and var, then learn to define and call functions before exploring function expressions and arrow syntax.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In JavaScript, a variable declaration creates a named binding for a value, while a function packages instructions that you can call and, when needed, use to return a result. For new code, use const when you will not reassign the binding and let when you will; learn var mainly so you can recognize its different scope behavior. Start with named function declarations, then learn function expressions and arrow functions.

What does a JavaScript variable do?

A variable gives a name to a value so your code can use it later. More precisely, a declaration creates a binding between an identifier and a value. Here, learnerName is bound to the string "Mina":

const learnerName = "Mina";

The declaration keyword matters: const, let, and var differ in whether the binding can be reassigned and in the scope rules that apply. MDN introduces these declaration forms in its JavaScript language overview and explains them in Grammar and types.

When should you use const, let, or var?

Choose based on whether you need to assign a different value to the same binding. In modern beginner examples, prefer const by default and change to let when reassignment is part of the logic. var is an older form with different scope behavior, and is usually not the best choice for new examples.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Declaration Can the binding be reassigned? Scope behavior
const No. Assign its initial value when declaring it. Block-scoped.
let Yes. Block-scoped.
var Yes. Not block-scoped; it does not follow the usual lexical scope rules.

Use let for a value you reassign

This counter starts at zero and then changes. Because the binding is reassigned, declare it with let:

let practiceCount = 0;
practiceCount = practiceCount + 1;

After the second line, practiceCount is 1.

Use const when the binding stays assigned to the same value

A const binding must be initialized when declared and cannot later be assigned a different value:

const learnerName = "Mina";
// learnerName = "Jo"; // TypeError: the binding cannot be reassigned

This restriction applies to the binding, not automatically to everything it refers to. An object declared with const can still have a property changed, because that operation does not reassign the binding:

const learner = { name: "Mina" };
learner.name = "Jo"; // The object changes; learner still refers to that object

If you need an object or array itself to be immutable, const alone does not provide that guarantee.

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

Why var behaves differently

let and const are block-scoped: their bindings are limited to the surrounding block, typically the code between a pair of curly braces. var is not block-scoped, so a declaration inside a block can be visible outside that block within its function scope or the surrounding script. That difference can cause surprises when code grows. MDN documents declaration and scope details in Grammar and types and Statements and declarations.

What is a JavaScript function?

A function is a reusable piece of code. You can call it by name, pass values called arguments, and use a return statement to send a result back to the caller. A function declaration begins with the function keyword:

function greet(name) {
  return `Hello, ${name}!`;
}

const message = greet("Mina");

In this example, name is the parameter in the function definition. The string "Mina" is the argument passed when calling greet. The function returns "Hello, Mina!", which is assigned to message.

A function can also perform an action without returning a useful value. If a function reaches its end without a return statement that supplies a value, its call evaluates to undefined. MDN covers declarations, calls, parameters, and return values in its Functions guide.

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

How do function expressions and arrow functions differ?

Functions can be values, too. That means you can assign one to a variable binding. First, here is a function expression assigned to a const:

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

const result = double(4); // 8

The function is created as part of an expression and assigned to double. The binding is available only after the declaration has run; calling it before that point is not equivalent to calling a function declaration earlier in its scope.

An arrow function is another function expression form. It can make small functions more compact:

const double = (number) => number * 2;

const result = double(4); // 8

When an arrow function has a single parameter, parentheses around that parameter can be omitted. For a body with multiple statements, use braces and an explicit return if it should produce a value:

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

Arrow functions are not interchangeable with ordinary functions

Arrow functions do not have their own this, arguments, super, or new.target. In particular, their this value comes from the surrounding context rather than being set by how the arrow function is called. That makes arrows useful in some callbacks, but not a universal shorter replacement for a function declaration or function expression. MDN explains arrow function behavior in its Functions guide.

Form How it is written Key distinction for a beginner
Function declaration function greet(name) { ... } Named declaration form; use it first to learn defining and calling functions.
Function expression const greet = function (name) { ... }; A function value assigned to a binding; the binding must be initialized before it can be called.
Arrow function const greet = (name) => ...; Compact expression syntax with lexical this and no own arguments, super, or new.target.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How these pieces fit together

Bindings hold values, including function values. A function can read an argument, calculate a result, and return it; another binding can keep that result for later use. This short example combines a reassigned counter, a named function declaration, and a function call:

let practiceCount = 0;

function addPractice(count) {
  return count + 1;
}

practiceCount = addPractice(practiceCount);

Once that pattern makes sense, try expressing a small function as a function expression and then as an arrow function. Keep the declaration keywords and function forms distinct: they solve related but different parts of writing JavaScript.

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.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver 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.