Variables Flashcards
What is a variable
A variable is simply a named area of a program’s memory space where the program can store data.
Typically, variables can be changed. That is, we can give the variable a new value.
What are variable names often referred to by the broader term?
Identifiers
In JavaScript, what do identifiers refer to?
Variable names declared by let and var
Constant names declared by const
Property names of objects (Global Objects)
Function names
Function parameters
Class names
What else is a variable?
Variables declared with let and var
Constants declared with const
Properties of the global object
Function names
Function parameters
Class names
Functions and classes are variable names?
Functions and classes are actually values in JavaScript, and their names are used in the same way as more traditional variables.
What is a variable declaration?
It is a statement that asks the JavaScript engine to reserve space for a variable with a particular name.
Optionally, it also specifies an initial value for the variable: undefined
What can we see from this?
From this we can see that the variable ‘firstName’ was initialized with a value of ‘undefined’
How can we assign a more useful value?
We do so by using an INITIALIZER in the declaration:
As you can see, we’ve now stored the string ‘Mitchell’ in memory. We can use firstName as a label for that string elsewhere in the program
Is ‘firstName’ permanently assigned to ‘Mitchell’?
NO! In fact, we can reassign it to any other value, not just strings:
What is the return value of an assignment?
It is the value on the right-hand side of the = operator
What do declarations return?
Declarations don’t actually return a value at all.
This is because declarations are statements. Statements don’t return values.
Note that regardless of whether we provide a value in a declaration, the variable is initialized. If we don’t provide an explicit value, that initial value is ‘undefined’
What are assignments and reassignments?
Expressions
Differences in terminology surrounding the ‘=’ operator
When used in a declaration, the = is just a syntactic token that tells JavaScript that you’re going to supply an initial value for the variable.
However, in an assignment, the = is called the assignment operator. For example, in let firstName = ‘Mitchell’, the = is a syntactic token, but in firstName = ‘Martha’, it is the assignment operator.
Do variables have values that are deeply linked to each other?
You’ll notice that b retains the value 4, even though a is now 7. This example suggests that variables have values that aren’t deeply-linked to each other. If you change one variable, it doesn’t change other variables with the same value.
‘const’ keyword?
The ‘const’ keyword is similar to ‘let’, but it lets you declare and initialize constant identifiers