Values and Variables Flashcards
What will
let reaction = 'yikes'; reaction[0] = 'l'; console.log(reaction);
print?
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.
Can you manipulate primities?
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.
Can you manipulate objects and arrays?
Yes.
How do you enter strict mode?
- ‘use strict’ at a file level
- ‘use strict’ at a function level
- any es6 module (i.e. being exported)
What is a good mental model for variables?
Wires.
Variables are not values. Variables point to values.
What are the two rules of variable assignment?
- 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
- 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 would you describe this?
console.log(pet);
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.
What does:
function double(x) { x = x * 2; }
let money = 10; double(money); console.log(money); // ?
log?
// 10
In JS you pass values not variables.
Do wires point to variables?
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.
Are variables values?
No. Variables are not values. Variables point to values. I.e. variables are made up of two things:
- A name
- A wire
Moreover, variables don’t have types in JavaScript — only values do.
What are the constituent parts of:
x = 10
- 1 Variable name
- 2 Variable wire
- is the variable and can be called a wire for short
- Expression
What will happen with the following code:
let answer = true;
answer. opposite = false;
console. log(answer.opposite);
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.
What will happen with the following code:
null = 10;
console.log(null);
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.
What can be mutated once passed into a function and what cant?
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!
If you have:
let pets = 'tess and bess' const renamePets = (pets) => { pets = 'foo and bar' }
will the outer pets variable change?
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.