JavaScript Variables

What Are JavaScript Variables?
Variables in JavaScript are used to store data values such as text, numbers, or objects.
Think of a variable like a container that holds information.
Declaring JS Variables
Js uses three keywords to declare variables:
| Keyword | Feature |
|---|---|
var | Old way, function-scoped |
let | Modern, block-scoped |
const | Constant (cannot be changed) |
Example:
Rules for Naming Variables
Must start with a letter,
_(underscore), or$Cannot start with a number
Cannot use JavaScript reserved words (like
class,return)JavaScript is case-sensitive (
name≠Name)
Example:
Assigning Values
You can declare first, then assign later:
Or declare and assign in one line:
Changing Variable Values
let and var values can be updated:
But const cannot be changed:
Js Variables Example with Output
Output:
Summary
| Keyword | Can Change Value? | Scope |
|---|---|---|
var | Yes | Function scope |
let | Yes | Block scope |
const | No | Block scope |
