Values and Variables Flashcards

1
Q

What will

let reaction = 'yikes';
reaction[0] = 'l';
console.log(reaction);

print?

A

Yikes!

‘yikes’ is a string - meaning its a primitive meaning it floats far beyond the reach of our code like a star in the sky - to be reference but never manipulated.

A string is not an array! Though there are some similarities.

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

Can you manipulate primities?

A

No - all primitive values are immutable. JS will throw an error in strict mode or silently not do what you ask in non-strict mode.

JS won’t let us set or mutate properties on primitives at all!

We can’t create, destroy, or change them.

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

Can you manipulate objects and arrays?

A

Yes.

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

How do you enter strict mode?

A
  • ‘use strict’ at a file level
  • ‘use strict’ at a function level
  • any es6 module (i.e. being exported)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a good mental model for variables?

A

Wires.

Variables are not values. Variables point to values.

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

What are the two rules of variable assignment?

A
  1. The left side of the assignment must be a ‘wire’ - i.e. must not be a value or expression.
let dog = 'woof' // good
let 'woof' = 'dog' // bad
  1. The right side of an assignment must be either an expression. Expressions can take the form of ‘2 + 2’ or just ‘2’ (the latter is called an expression literal or literals for short) - the expression must always yield a value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you describe this?

console.log(pet);

A

Passing the current value of ‘pet’ to console.log function. I.e. we’re passing an expression of ‘pet’ to the log function - this evaluates at run time to the current value of ‘pet’. There is no concept of ‘pet’ at run time. It just follows the wire.

Note we’re not actually passing ‘pet’ as variables are not real things - they just point to things.

TLDR you cannot pass a variable in JS.

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

What does:

function double(x) {
  x = x * 2;
}
let money = 10;
double(money);
console.log(money); // ?

log?

A

// 10

In JS you pass values not variables.

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

Do wires point to variables?

A

No.

Wires point to expressions which evaluate to values. Wires never point to variables.

a = 10
b = a
a = 0

b === 10 // true

b points to the expression that a is wired to at the time of assignment. It doesn’t keep following that wire from a every time it is evaluated - b becomes the expression that a points to.

Basically: We can’t point variables to each other! Variables always point to values.

No boxes only wires.

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

Are variables values?

A

No. Variables are not values. Variables point to values. I.e. variables are made up of two things:

  1. A name
  2. A wire

Moreover, variables don’t have types in JavaScript — only values do.

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

What are the constituent parts of:

x = 10

A
  1. 1 Variable name
  2. 2 Variable wire
  3. is the variable and can be called a wire for short
  4. Expression
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will happen with the following code:

let answer = true;

answer. opposite = false;
console. log(answer.opposite);

A

This code won’t be able to set a property.

Booleans are primitive. All primitive values are immutable. We can’t change them — and setting a property is a change.

If our code runs in the strict mode, setting a property on a primitive value will error. Otherwise, the assignment is ignored.

In either case, we can’t set a property on a boolean like true.

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

What will happen with the following code:

null = 10;
console.log(null);

A

It will always produce an error.

The left side of the assignment must always be a “wire”. Variables are “wires”, so they can appear on the left side. A literal like null is not a “wire”, so trying to assign something to it is meaningless.

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

What can be mutated once passed into a function and what cant?

A

Primitives can’t be changed at all.

The variables you placed in parenthesis cannot be changed because they are not passed in - the expression values they are wired to are what is actually passed in.

Non primitives can be changed such as arrays and objects.

Basically: Whenever you pass a mutable value, someone can mess it up!

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

If you have:

let pets = 'tess and bess'
const renamePets = (pets) => {
  pets = 'foo and bar'
}

will the outer pets variable change?

A

No it won’t. You pass in expressions not variables. pets inside the function scope is an entirely new variable.

The pets argument in the feed function has nothing to do with the pets variable in your code. They are two different “wires”. However, they both point to the same value.

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