Content updated
By the end, you can…
- Use const when the binding stays the same, and let when you need to assign a new value.
- Predict the example's result, change one part, and verify the behavior in your own script.
One idea at a time
A variable connects a name to a value; that connection is called a binding. Declaration introduces the name; assignment gives it a value. Choose a name that tells a reader what the value means, such as lessonCount rather than x. JavaScript is dynamically typed: values have types, and a let binding can later hold a different type. Consistent types still make your program easier to understand.
Strings represent text, numbers represent numeric values, and booleans are true or false. undefined often means no value has been assigned; null is an intentional absence chosen by your code. An empty string is still a string, and zero is still a number. Objects and arrays, introduced later, hold collections rather than just one simple value.
What to remember first
Use const when the binding stays the same, and let when you need to assign a new value.
Read the code before trying it
const name = valueDeclare a stable binding- Supply its value now. You cannot assign a different value to this name later.
let count = 1Declare a reassignable binding- Use count = 2 later without another let. = assigns; it does not compare.
typeof valueInspect a type- typeof 3 is "number". A historical exception is typeof null, which returns "object".
{ ... }Create a block scope- A let or const declared inside a block belongs to that block. An outer name can be read inside it.
varRecognize older declarations- var has function scope rather than block scope. Prefer let and const while learning.
Walk through an example
const learner = "Mia";
let completed = 1;
completed = completed + 1;
console.log(learner);
console.log(completed);
console.log(typeof completed);learner keeps the text Mia. completed starts as the number 1; the assignment reads that old value, adds one, and stores 2. Logging typeof completed produces number. We use const for the unchanged name and let for the count. Neither declaration displays anything until console.log runs.
See it happen: try a small change
Predict the result, choose a version, then compare the code and output. Try the controls when the example has them. Each version starts fresh.
Code to notice
let count = 1;
count = count + 1;
console.log(count);
console.log(typeof count);Live result
What changed: Reassignment changes count to 2 while its value remains a number.
Look a little closer
Scope describes where a name can be used. A nested block can read an outer variable. Declaring a new name with the same spelling inside that block shadows the outer binding: it does not replace it. Compare the scope variant and follow the two separate values. Outside the block, its local declaration is no longer available.
const protects the binding, not every value inside an object. A const array can still gain an item, and a const object can still have a property changed. You will revisit this when learning objects. Do not replace every const with let just to allow an object's property to change.
Try it in your own project
Replace the previous script with this example instead of appending duplicate declarations. Save and reload. Predict the two counts before trying the block-scope version; changing the inner count should not change the outer count.
The interactive preview runs in its own isolated document. Its Output panel mirrors console.log messages; it is not a full DevTools console. Choosing another version or resetting starts a fresh example and does not edit your local files. Read any simulation label: storage and request demonstrations explain their offline substitutes, while the written local examples use the real browser APIs.
A common wrong turn
Using a name before its let or const declaration runs causes an error. Re-declaring the same const in the same script also fails. Run each variant as a separate script; the preview resets automatically. In your file, replace the previous example instead of stacking declarations with identical names.
Your turn: store values with variables and data types
Track a learner's name and completed lessons, then explain which binding needs let.
Optional exploration
Try the related prediction challenge below, or run the task above in your own script. The challenge asks you to choose an output and explains the result.
- Declare a constant name and a count starting at zero.
- Increase the count twice and log its value and type. Expect 2 and number.
- Put a different count inside a block, log it inside, and log the original outside. Explain why the two values differ.
Go to the source
Examples are authored for Tuklas and checked with code and reference documentation. This is not a claim of independent human review. Suggest a correction.