JS Variables
JavaScript variables are containers for storing data values.
Table of Contents
Variables are named containers that store values you can reuse throughout your program. JavaScript gives you three keywords to declare them.
| Keyword | Reassignable | Scope | Use When |
|---|---|---|---|
const | โ No | Block | Value wonโt change (recommended default) |
let | โ Yes | Block | Value will change |
var | โ Yes | Function | Old code โ avoid in modern JS |
Try It โ Variables in Action
Preview
Variable Naming Rules
- Must start with a letter,
$, or_ - Can contain letters, digits,
$,_ - Case sensitive โ
nameโName - Cannot be a reserved keyword (
let,if,return, etc.)
Tip: Good variable names describe what the value means:
userName,totalPrice,isLoggedIn. Avoid single letters likexor vague names likedata.