Variables Flashcards

1
Q

What is a variable

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are variable names often referred to by the broader term?

A

Identifiers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In JavaScript, what do identifiers refer to?

A

Variable names declared by let and var

Constant names declared by const

Property names of objects (Global Objects)

Function names

Function parameters

Class names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What else is a variable?

A

Variables declared with let and var

Constants declared with const

Properties of the global object

Function names

Function parameters

Class names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Functions and classes are variable names?

A

Functions and classes are actually values in JavaScript, and their names are used in the same way as more traditional variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a variable declaration?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What can we see from this?

A

From this we can see that the variable ‘firstName’ was initialized with a value of ‘undefined’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we assign a more useful value?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is ‘firstName’ permanently assigned to ‘Mitchell’?

A

NO! In fact, we can reassign it to any other value, not just strings:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the return value of an assignment?

A

It is the value on the right-hand side of the = operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do declarations return?

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are assignments and reassignments?

A

Expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Differences in terminology surrounding the ‘=’ operator

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Do variables have values that are deeply linked to each other?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

‘const’ keyword?

A

The ‘const’ keyword is similar to ‘let’, but it lets you declare and initialize constant identifiers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Are constants immutable?

A

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

17
Q

What is the standard convention when naming constants?

A

It is to use all uppercase letters and digits in the name; if the name contains multiple words, use underscores to separate the words.

18
Q

Do ‘const’ declarations require a value?

A

Yes!

19
Q

What is ‘variable scope’?

A

Determines where it is available in the program

The location where you declare a variable determines its scope

20
Q

What is ‘block scope’?

A

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

21
Q

Is everything between curly braces technically a block?

A

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

22
Q

In general, where do blocks appear?

A

Blocks appear in if…else, while, do…while, for, switch, and try…catch statements, or by themselves (as in the first example above)

23
Q

Why does the console print ‘bar’?

A

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.

24
Q

Does the third type of variable declaration ‘var’ use block scoping?

A

NO!

25
Q

In JavaScript, can you create a variable by merely assigning a value to it without declaring it variable or constant?

A

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.