JavaScript Tutorial

JavaScript Variables

A variable is a named box. Modern JavaScript uses let and const. var is the old keyword.

A named box

A variable holds a value under a name. You declare the name, put a value in, then read it later. The name stays. The value can change if you used let.

Example

let score = 10;
console.log(score);
score = 15;
console.log(score);

score is the name. 10 is the first value. The second assignment replaces it with15.

Run this in /javascript/try with Try it in JavaScript. Watch the console pane.

let, const, and var

Three keywords declare a name. Use let when the value will change. Use const when you will not rebind the name. Do not use var in new code.

KeywordRebind the name?Scope
letYesThe block it is in
constNoThe block it is in
varYesThe function it is in — old rules

The next two chapters cover let and const in full. This page is the map.

Declare, then assign

You can declare and assign in one line. You can also declare first and assign later with let. Until you assign, the value is undefined.

Example

let title;
console.log(title);
title = "StudyGrid";
console.log(title);

const cannot wait. It needs a value on the same line as the declaration. That is one reason beginners start with let and then switch names to const when the value is known and fixed.

Assignment replaces the value

= stores a value in a name that already exists. It is not the same as ===, which compares. Read = as “put this in that box.”

Example

let count = 0;
count = count + 1;
count = count + 1;
console.log(count);

Each assignment uses the current value, adds one, and stores the result. After two updates, countis 2.

Why not var

var is function-scoped and can be redeclared in the same scope. Those rules surprise people.let and const are block-scoped and refuse a second declaration in the same block.

Example

var n = 1;
var n = 2;
console.log(n);

That second var n is allowed. The same pair with let is a syntax error. Prefer the stricter keywords. You will still see var in old pages and old answers.

This tutorial uses let and const only. If a snippet on the web starts withvar, rewrite it.

Names

JavaScript is case-sensitive: Score is not score. Start with a letter, underscore, or $. Do not use a reserved word such as let or return.

GoodPoor
itemCount, taxRate, nx1x2, data, temp2 with no context

Common style in JavaScript is camelCase: itemCount, not item_count. Both are legal. Pick one style and keep it.

A type can change

JavaScript does not lock a name to one type. A let that held a number can later hold a string. That is legal. It is also a source of bugs. Keep one kind of value in one name.

Next: let — reassignment and block scope.

FAQ: JavaScript Variables

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript variables examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript variables in this JavaScript JavaScript lesson (JavaScript Variables).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.