JavaScript: JavaScript Variables

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Dana🏷 Category: JavaScript Tutorial

Variables are boxes for storing data — you label each box with a name, then put data in, take data out, or swap it anytime.

Related Statements:function const let var for

1. What Are Variables

Imagine a row of labeled boxes: one called "name" holds "John," another called "age" holds 25. The contents can change, but the label stays the same — that's a variable.

In JS, you create variables using declaration keywords (var / let / const) and assign values with the equals sign:

HTML
<!-- Script tag -->
<script>
var name = "John";
let age = 25;
const PI = 3.14;
</script>

2. The Problems with var

var was the only way to declare variables before ES6. It has two headache-inducing behaviors:

  1. Function scope — a variable declared with var inside an if or for block is accessible outside, which is counterintuitive
  2. Hoisting — var declarations are "hoisted" to the top of their scope, but assignments are not. You can use a variable before its declaration (its value will be undefined) without errors, which leads to subtle bugs

These two behaviors make var a breeding ground for bugs. In modern JS, var has been largely replaced by let and const.


let was introduced in ES6 (2015) and fixes both problems with var:

In daily development, use let for variables that need to be reassigned.


4. const — Constants, Cannot Be Reassigned

const also has block scope, but it must be assigned at declaration and cannot be reassigned afterward.

Note: const restricts "reassignment," not "mutation." If a const holds an object or array, you can modify object properties or array elements — you just can't point the variable to a different object.

Use const by default; switch to let when you need to reassign — this is the best practice.


5. Variable Naming Rules


6. Declaring Multiple Variables

You can declare multiple variables at once, separated by commas:

HTML
<!-- Script tag -->
<script>
let a = 1, b = 2, c = 3;
</script>

However, for readability, one variable per line is preferred.

▶ Example: var Hoisting and Function Scope

▶ Try it Yourself

▶ Example: let Block Scope

▶ Try it Yourself

▶ Example: const Constants

▶ Try it Yourself

▶ Example: var / let / const Comparison

▶ Try it Yourself

Related Statements:function const let var for

❓ FAQ

Q Which keyword should you use to declare a variable in JavaScript, and what is the difference between let, const, and var?
A Modern JavaScript uses let and const to declare variables, while var is the older style. Variables declared with let can be reassigned, variables declared with const cannot be reassigned, and var does not have block scope. For new code, prefer const first and use let when the value needs to change.
Q Why does a declared JavaScript variable report undefined or ReferenceError?
A undefined usually means the variable has been declared but not yet assigned a value, such as reading a right after let a;. A ReferenceError means you accessed a variable that was never declared, or accessed a let/const variable before its declaration (the temporal dead zone). Check the declaration location and spelling to locate the issue.
Q How do you output a JavaScript variable value to the page or convert it to a string?
A You can concatenate with template strings, such as `Hello, ${name}`, or explicitly convert with String(value) or value.toString(). To output to the page, it is common to use document.getElementById('app').textContent = name, which is safer than directly concatenating HTML.

📖 Summary

📝 Exercises

  1. Basic: Declare a constant SITE_NAME with your name using const, and a variable visitCount set to 0 using let, then display both values on the page.
  2. Intermediate: Create a page with a button that simulates a "visit counter" — each click increments visitCount by 1 and displays the current value. Notice how let allows reassignment while const doesn't.
  3. Challenge: Create a page with a const student object { name: "John", scores: [80, 90, 75] }. Then add a new score of 85, calculate the average, and change the name to "Jane" — all without reassigning the const, proving that object contents can be modified.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏