JavaScript Basics
Variables
Variables are named containers for values. JavaScript has three keywords to create them.
Syntax:
const name = "Ayesha"; // can't be reassigned
let score = 10; // can be reassigned
score = score + 5;
console.log(name, score);Output
Ayesha 15
| Keyword | Reassign? | Scope | Use it? |
|---|---|---|---|
const | No | Block | Yes, by default |
let | Yes | Block | Yes, when the value changes |
var | Yes | Function | No, it's the old way |
Naming rules
- Names can contain letters, digits,
_and$, but can't start with a digit. - Use camelCase:
firstName,totalPrice,isLoggedIn. - Choose meaningful names:
priceis better thanporx. - You can't use reserved words like
let,iforfunction.