JS Fundamentals - Variables Flashcards
to declare variable
use let
multi line variabes
let user = 'John', age = 25, message = 'Hello';
Or even in the “comma-first” style:
let user = 'John' , age = 25 , message = 'Hello';
variable namings
The name must contain only letters, digits, or the symbols $ and _.
The first character must not be a digit.
case matters
Variables named apple and AppLE are two different variables.
Non-Latin letters are allowed, but not recommended
let имя = ‘…’;
let 我 = ‘…’;
Reserved names
There is a list of reserved words, which cannot be used as variable names because they are used by the language itself.
For example: let, class, return, and function are reserved.
The code below gives a syntax error:
constants
const myBirthday = ‘18.04.1982’;
uppercase constants
Uppercase constants
Being a “constant” just means that a variable’s value never changes. But there are constants that are known prior to execution (like a hexadecimal value for red) and there are constants that are calculated in run-time, during the execution, but do not change after their initial assignment.
For instance:
const pageLoadTime = /* time taken by a webpage to load */; The value of pageLoadTime is not known prior to the page load, so it’s named normally. But it’s still a constant because it doesn’t change after assignment.
In other words, capital-named constants are only used as aliases for “hard-coded” values.
Summary
let – is a modern variable declaration.
var – is an old-school variable declaration. Normally we don’t use it at all, but we’ll cover subtle differences from let in the chapter The old “var”, just in case you need them.
const – is like let, but the value of the variable can’t be changed.
Variables should be named in a way that allows us to easily understand what’s inside them.