* variIn JavaScript, variables are used to store data values. There are three main ways to declare variables: var, let, and const. Each has different rules and behaviors. Here's a breakdown:
---
1. var
Function-scoped
Can be re-declared and updated
Hoisted (moved to the top of its scope, but not initialized)
var x = 5;
var x = 10; // Valid
x = 15; // Valid
---
2. let
Block-scoped (inside { })
Can be updated, but not re-declared in the same scope
Hoisted but not initialized
let y = 5;
y = 10; // Valid
// let y = 15; // Error: already declared in the same scope
---
3. const
Block-scoped
Cannot be updated or re-declared
Must be initialized at declaration
const z = 5;
// z = 10; // Error: assignment to constant variable
// const z = 15; // Error: already declared
---
General Rules:
Variable names cannot start with numbers
Can contain letters, digits, underscores (_), and dollar signs ($)
JavaScript is case-sensitive (name ≠ Name)
Avoid using reserved words as a variable name.
- Teacher: Admin User