What is Variables?
- Variables are the container that are used to store the value.
- The stored values can be accessed, updated, and reused whenever needed.
Rules for Naming Variables in JavaScript
- Variable names can contain letters, digits, _ (underscore), and $ (dollar sign).
- Variable names must start with a letter, _, or $.
- Variable names cannot contain spaces.
- Variable names cannot use special characters except _ and $.
- Variable names are case-sensitive.
- Reserved keywords cannot be used as variable names.
There are three ways to declare variables:
- Let
- Const
- Var
Important things about variables:
1. Variable Declaration:
Creating a variable using let, const, or var.
let and var can be declared first and initialized later.
const must be initialized at the time of declaration.
let name;
const pi = 3.14;
var age;
2. Variable Initialization
Assigning a value to a variable.
name="Cat";
age=22;
3. Variable Reassignment
Changing the value of a variable.
var age = 22;
age = 23;
let name="Cat";
name="Parrot";
Excited to continue learning and building with JavaScript!
Top comments (0)