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
Are constants immutable?
Constants have an immutable binding to their values. Unlike an ordinary variable, once you declare a constant, you cannot assign it a new value. The constant will continue to have that value until the constant is no longer needed.
Using constants is a great way to label a value with a name that makes your code more descriptive and easier to understand
What is the standard convention when naming constants?
It is to use all uppercase letters and digits in the name; if the name contains multiple words, use underscores to separate the words.
Do ‘const’ declarations require a value?
Yes!
What is ‘variable scope’?
Determines where it is available in the program
The location where you declare a variable determines its scope
What is ‘block scope’?
A block is a related set of JavaScript statements and expressions between a pair of opening and closing curly braces.
The term block usually refers to executable code between braces, including function bodies
Is everything between curly braces technically a block?
No!
For instance, the braces that surround an object literal do not define a block.
Technically, the braces that surround a function body don’t define a block either, though it is convenient to think of function bodies as blocks.
While there are similarities between blocks, function bodies, and, to a lesser degree, object literals, the term block usually refers to executable code between braces, including function bodies
In general, where do blocks appear?
Blocks appear in if…else, while, do…while, for, switch, and try…catch statements, or by themselves (as in the first example above)
Why does the console print ‘bar’?
This code prints the string “bar” since a is accessible inside the block. Thus, we can reassign it to a different value inside the block
Variables declared with ‘const’ have the same scope as ‘let’ variables.
Does the third type of variable declaration ‘var’ use block scoping?
NO!
In JavaScript, can you create a variable by merely assigning a value to it without declaring it variable or constant?
YES!
That looks harmless, but JavaScript has some bizarre rules when working with undeclared variables. The most notable rule is that all undeclared variables have global scope: they ignore block and function scope entirely. If your program uses that same variable name in a different scope without declaring it, there’s a good chance that it will step on the original variable by changing its content. You don’t want that to happen: it’s typically difficult to debug, and sometimes fixing it breaks other code.