A variable is a “named storage” for values or data
- Use real-life analogy of a named ingredients in a kitchen
- JavaScript application needs to work with information. E.g Catalogue list holding shopping items, Chat application holding messages, names etc
- Create a variable using the let and const keyword. We’ll take about their difference later.
- Can declare multiple variables in single line separated by comma
- Talk about var and its availability in older script. Discourage it’s usage
Variables (Naming conventions)
- The name must contain only letters, digits, or the symbols $ and _ . Required
- The first character must not be a digit. Required
- Use camelCase when variables contain multiple words Optional
- Case matters: Variables named apple and AppLE are two different variables.
- Reserved Names/Keyword cannot be used. E.g let let = 5; let return = 5;
Variables (const)
- Variables declared with const do not change and cannot be re-assigned
- It is a convention in JS to CAPITALIZE const variable names
- Separate multiple words with under_score
Variables (let)
- Variables declared with let can change and be re-assigned
- Variable names should be descriptive and it’s intention should be clear
- It is recommended to use camelCase for long variables
- A variable name should have a clean, and meaning obvious.
- Variable naming is one of the most important and complex skills in programming.
- Some good-to-follow rules are:
- Use human-readable names like userName or shoppingCart.
- Rarely use abbreviations or short names like a, b, c
- Make names maximally descriptive and concise. Examples of bad names are data and value..
- Agree on terms within your team and in your own mind. If a site visitor is called a “user” then we should name related variables currentUser or newUser instead of currentVisitor or newManInTown.