Variables and values
Store numbers and text in variables with let and const, and name them well.
Storing values
const church = 'RCCG';
let members = 120;
members = members + 5;
console.log(church + ' has ' + members + ' members');
RCCG has 125 members
| Keyword | Use it when |
|---|---|
| const | The value is never reassigned (use it by default) |
| let | The value will change later |
| var | Old code only; avoid |
Naming rules
- Names can contain letters, digits,
_and$but cannot start with a digit. - Use camelCase:
firstName,totalScore. - Choose names that say what the value is:
studentCount, notx.
Exercise
Store your name and age in variables, then print a sentence like "Ada is 17 years old."
// your variables here
Show a possible solution
const name = 'Ada';
let age = 17;
console.log(name + ' is ' + age + ' years old.');
Check your understanding
Quick check
Which keyword should you use for a value that never changes?
Why: const prevents reassignment by mistake.
Quick check
Which is a valid variable name?
Why: No leading digit and no hyphen.
Quick check
What does camelCase look like?
Why: First word lowercase, next words capitalised.