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.

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

In JavaScript, variables are bindings to values, functions are callable values, and objects group properties behind references. Use const when a binding will not be reassigned, let when it will, and recognize var as older syntax with different scoping rules. Understanding those distinctions helps explain why a block may hide a variable, why a function can remember outer values, and why changing an object through one variable can be visible through another.

How JavaScript variables 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 at different times.

Choose between const and let by reassignment

Use const when you will not assign a different value to the binding. It must be initialized when declared. Use let when the binding needs to receive a new value; an uninitialized let starts as undefined. MDN’s practical guidance is “Use const when you can, and use let when you have to.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const user = { name: "Ari" };
user.name = "Sam"; // allowed: the binding still refers to the same object

let count = 0;
count += 1; // allowed: count is assigned a new number value

const prevents reassignment of the binding; it does not freeze the value or recursively freeze data reachable through it. The object example can have its property changed because user still refers to the same object. See MDN’s JavaScript grammar and types guide.

How let, const, and var differ

Declaration Scope Can the binding be reassigned? Initializer required? Access before declaration is reached?
let Block scope Yes No; without one, its initial value is undefined. No. Access during the temporal dead zone throws a ReferenceError.
const Block scope No Yes No. Access during the temporal dead zone throws a ReferenceError.
var Function scope, or global scope when declared outside a function; not block scope Yes No Its behavior differs from let and const; it does not use their temporal dead zone.

A block is code enclosed in braces, such as an if body. A let or const declared there is available only within that block. A var declared in the same block remains available in its enclosing function or, outside a function, the global scope.

if (true) {
  let blockValue = "inside";
  var functionValue = "still in the enclosing scope";
}

// blockValue is not available here
console.log(functionValue);

JavaScript’s “temporal dead zone” is the period from entering a scope until execution reaches a let or const declaration. Trying to read that binding in the meantime throws a ReferenceError, even if an outer scope has a variable with the same name. MDN documents these declaration and scope rules in its grammar and types guide and JavaScript language overview.

var is legacy syntax that appears in existing code. For new code, block-scoped let and const make the lifetime of a binding easier to see.

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

What JavaScript functions do

A function is callable code that can accept inputs and return a result. Functions are also values: they can be stored in variables, passed as arguments to other functions, and returned from functions. This lets code package behavior for reuse instead of repeating the same instructions.

function add(a, b) {
  return a + b;
}

const operation = add;
console.log(operation(2, 3)); // 5

How closures use an enclosing scope

A nested function can access bindings from the function that encloses it. When that inner function continues to use those bindings, this behavior is called a closure.

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

const greetAri = makeGreeter("Ari");
console.log(greetAri()); // Hello, Ari

The returned function can still read name after makeGreeter has finished. Closures let helper behavior stay near the code that uses it without placing every helper in a wider scope. Scope works in the other direction too: an enclosing function cannot access a variable declared only inside its nested function. For further detail, see MDN’s guide to closures and beginner guide to functions.

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

How JavaScript objects and references work

An object groups properties. Read a property with dot notation when its name is a valid identifier, as in user.name, or bracket notation, as in user["name"]. Brackets are useful when the property name is stored in a variable or is not a simple identifier.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const user = { name: "Ari" };
const property = "name";

console.log(user.name);       // Ari
console.log(user["name"]);    // Ari
console.log(user[property]);  // Ari

Assignments copy object references, not objects

When you assign an object to another binding, JavaScript copies the reference to that object, not a separate object. Both bindings can therefore observe a mutation made through either one.

const first = { score: 1 };
const second = first;
second.score = 2;

console.log(first.score); // 2

By contrast, two separately created object literals are distinct objects, even if their properties and values match. The practical distinction is identity: matching contents do not mean two object values are the same object. MDN explains object properties and references in its JavaScript language overview.

Putting the concepts together

  • Use const for a binding you will not reassign; use let for one that needs a new value.
  • Remember that const protects the binding, not the contents of an object it refers to.
  • Use let and const when block scope is intended; understand that var escapes ordinary block boundaries.
  • Treat functions as both callable behavior and values that can be passed or returned.
  • When two bindings refer to one object, a property mutation through either binding is visible through both.

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.