DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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

JavaScript Variables, Functions, and Objects: A Beginner’s Guide

A beginner’s guide to JavaScript bindings, scope, reassignment, functions, closures, object properties, and shared references.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScript variables are bindings to values, functions are values that can also be called, and objects group properties that may be shared through references. For everyday code, use const when a name will not be reassigned and let when it will; understand var mainly because it appears in older code.

How JavaScript variables and bindings work

A variable is a named binding that lets code refer to a value. JavaScript is dynamically typed: a binding declared with let can refer to values of different types over time.

As an Amazon Associate I earn from qualifying purchases.

let value = 3;
value = "three"; // allowed: the binding now refers to a string

Choose a declaration according to whether the binding needs to point to a different value later. MDN’s practical guidance is: “Use const when you can, and use let when you have to.”

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.

const for bindings you will not reassign

A const declaration must have an initializer, and its binding cannot be assigned a different value later. This does not make the value itself immutable. If the value is an object, its properties can still change.

const user = { name: "Ari" };
user.name = "Sam"; // allowed: the binding still refers to the same object

let for bindings that need a new value

A let binding can be reassigned. If declared without an initializer, it starts with the value undefined.

let count = 0;
count += 1; // allowed: count is reassigned

var in older code

var is legacy declaration syntax that readers will encounter. Unlike let and const, it is scoped to a function or, outside a function, the global scope—not to an ordinary block. As a result, a var declared inside an if block can still be available after that block, within its enclosing function or global scope. MDN uses let and const in its examples. See MDN’s guide to grammar and types and its JavaScript language overview.

How scope affects access to variables

Scope determines where a binding can be accessed. let and const are block-scoped: a binding declared inside braces belongs to that block. var is function-scoped or global-scoped, so ordinary braces do not limit its availability.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if (true) {
  let insideBlock = "here";
  var outsideBlock = "still available";
}

// insideBlock is not available here
console.log(outsideBlock); // "still available"

Declarations and the temporal dead zone

A let or const binding cannot be accessed until execution reaches its declaration. Trying to read it earlier in the same scope throws a ReferenceError; this interval is called the temporal dead zone.

console.log(name); // ReferenceError
let name = "Ari";

An outer binding with the same name does not make that early access fall back to the outer value: the inner binding is already the relevant name in its scope, but it is not initialized yet. For the declaration and scope rules, see MDN’s grammar and types guide.

What functions do—and why they are values

A function is callable code that can take inputs and return a result. Functions are also values: they can be stored in variables, passed to other functions as arguments, and returned from other functions.

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

const sayHello = greet;
console.log(sayHello("Ari")); // "Hello, Ari"

Here, sayHello refers to the same function as greet; assigning it does not call the function. Parentheses with an argument, as in sayHello("Ari"), call it. For a beginner-focused explanation, see MDN’s functions module.

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

Nested functions and closures

A function declared inside another function can access bindings from its enclosing scope. A function together with the surrounding bindings it can access is called a closure. This lets related helper behavior stay near the code that uses it.

function makeGreeter(greeting) {
  return function (name) {
    return `${greeting}, ${name}`;
  };
}

const greet = makeGreeter("Welcome");
console.log(greet("Ari")); // "Welcome, Ari"

The returned inner function can still read greeting from the call to makeGreeter. Scope works outward in this example: the inner function can use an enclosing binding, but the enclosing function cannot access a variable declared only inside the inner function. See MDN’s closures guide.

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

How objects, properties, and references work

An object groups related properties. Read a property with dot notation when its name is a simple identifier, or bracket notation when the name is held in a variable or needs to be expressed as a string.

const user = { name: "Ari", score: 1 };
console.log(user.name); // "Ari"
console.log(user["name"]); // "Ari"

const property = "score";
console.log(user[property]); // 1

Assignment shares an object reference

When one variable is assigned an object held by another, the assignment copies the reference, not the object. Both bindings then refer to the same object, so a change made through either is visible through the other.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const first = { score: 1 };
const second = first;
second.score = 2;
console.log(first.score); // 2

Two separately created object literals are distinct objects, even if they contain identical properties. Also, const prevents rebinding a name; it does not freeze the object reachable through that name. These reference and property basics are covered in MDN’s JavaScript language overview.

A practical rule for writing and reading JavaScript

  • Use const when you will not reassign the binding, including when it refers to an object whose properties may change.
  • Use let when the binding needs to be reassigned.
  • Recognize var as function-scoped or global-scoped syntax in older code; braces alone do not contain it.
  • Remember that functions can be passed around as values and that nested functions can retain access to enclosing bindings.
  • When an object is assigned to another binding, expect both names to refer to the same object unless you explicitly create a copy.

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

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.