Last modified: May 26, 2026
JavaScript Variables in HTML
JavaScript variables store data. You can use them inside HTML. This makes web pages interactive.
Variables hold values like numbers, text, or objects. You declare them with let, const, or var.
In this guide, you will learn how to use JavaScript variables in HTML. We will cover declaration, scope, and practical examples.
What Are JavaScript Variables?
A variable is a container for data. It has a name and a value. You can change the value later.
Example: let userName = "Alice"; stores the string "Alice" in the variable userName.
Variables make code reusable. You can use the same variable in many places.
How to Declare JavaScript Variables
Use let, const, or var. Each has different rules.
- let – for values that can change.
- const – for fixed values.
- var – older way, avoid in modern code.
Example:
// Declare variables
let age = 25;
const birthYear = 1998;
var name = "Bob"; // old style
Always use let or const. They are safer.
Using Variables in HTML
You can display variables inside HTML elements. Use JavaScript to update the content.
First, add an element with an id. Then, use document.getElementById() to change its text.
Example:
<p id="demo"></p>
<script>
let message = "Hello, World!";
document.getElementById("demo").innerHTML = message;
</script>
Output:
Hello, World!
The variable message holds the string. The script puts it into the paragraph.
Variable Scope in HTML
Scope decides where a variable works. Global variables work everywhere. Local variables work only inside a function.
Example:
// Global variable
let globalVar = "I am global";
function myFunction() {
// Local variable
let localVar = "I am local";
console.log(globalVar); // works
console.log(localVar); // works
}
console.log(globalVar); // works
// console.log(localVar); // error
Use local variables to avoid conflicts. Read more about JavaScript Variable Scope Explained.
Variable Types in JavaScript
Variables can hold different types of data. Common types are:
- String – text, e.g., "Hello"
- Number – numbers, e.g., 42
- Boolean – true or false
- Array – list of values
- Object – collection of properties
Example:
let name = "Alice"; // string
let age = 30; // number
let isStudent = true; // boolean
let colors = ["red", "green", "blue"]; // array
let person = {firstName: "John", lastName: "Doe"}; // object
JavaScript is dynamically typed. You don't need to declare the type.
Using Variables with HTML Forms
Variables can capture user input from forms. This makes pages interactive.
Example:
<input type="text" id="nameInput" placeholder="Enter your name">
<button onclick="greet()">Greet</button>
<p id="greeting"></p>
<script>
function greet() {
let userName = document.getElementById("nameInput").value;
let greeting = "Hello, " + userName + "!";
document.getElementById("greeting").innerHTML = greeting;
}
</script>
When the user clicks the button, the variable userName gets the input value.
Best Practices for Variables in HTML
Follow these tips for clean code:
- Use descriptive names like
userAgeinstead ofx. - Use
constby default. Only useletwhen you need to change the value. - Avoid global variables. They can cause bugs.
- Always declare variables before using them.
For a deeper look, check the JavaScript Variables Guide.
Common Mistakes with Variables
Beginners often make these errors:
- Forgetting to declare a variable. This creates a global variable.
- Using
varwhenletorconstis better. - Mixing up variable names. JavaScript is case-sensitive.
- Trying to change a
constvariable. This causes an error.
Example of a mistake:
// Wrong: missing declaration
myVar = 10; // becomes global
// Wrong: changing const
const pi = 3.14;
// pi = 3.14159; // error
Always use let or const. See the JavaScript Variable Declaration Guide for more.
Conclusion
JavaScript variables are essential for dynamic web pages. You learned how to declare them, use them in HTML, and manage scope.
Start with let and const. Practice with simple examples. Soon you will build interactive websites easily.
Remember: variables store data. They make your code flexible and powerful. Keep coding!