U3LA3.1 JavaScript Mutability
What structures can I use to store and manipulate larger and more nuanced data?
Teacher Notes & Overview
This is a vanilla javascript lab. It takes place solely in the console and is aimed at teaching students about mutability. Students will learn when an assignment is passed-by-reference vs when it is passed-by-value, and they will learn a "shallow copy" vs a "deep copy".
These tools are helpful debugging tools and not essential skills that should be graded or memorized. This lesson can be framed as a debugging session or an exploration -- a chance to review certain topics and understand other concepts deeper and with more context.
Depending on the level of your specific classroom, this lesson can take 1 to 2 days. The long lesson demo is sectioned off into smaller portions. You can break these up over a couple of days ending the day with an "Exploration" of let
vs const
with the day's material thus far (see Student Exploration below).
Objectives
Students will be able to:
Intentionally make mutable and immutable variables.
Debug code with objects that have unintentionally been made mutable.
Use assignment operations that pass-by-value and pass-by reference.
Make deep copies and distinguish them from shallow copies
Explain the difference between "let" and "const" in relation to mutability.
Suggested Duration
1-2 periods (45-90 minutes)
NYS Computing Standards
9-12.CT.4 Implement a program using a combination of student-defined and third-party functions to organize the computation.
9-12.CT.7 Design or remix a program that utilizes a data structure to maintain changes to related pieces of data.
9-12.DL.1 Type proficiently on a keyboard.
9-12.DL.2 Communicate and work collaboratively with others using digital tools to support individual learning and contribute to the learning of others.
9-12.DL.5 Transfer knowledge of technology in order to use new and emerging technologies on multiple platforms.
Vocabulary
pass-by-reference: a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. (ie. pass the entire reference of the data)
pass-by-value: you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter (ie. pass the data itself)
spread operator: expands iterable into individual elements. The spread operator is commonly used to make deep copies of JS objects.
mutable object: An object that allows you to change its values without changing its identity
deep copy: a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made. (ie. released of all variable references)
shallow copy: a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made.
memory location: location in computer memory that holds data in the form of a binary number or a decimal number of some sort.
Planning Notes && Materials
Suggestions for UDL
Suggestions for Differentiation
Students have opportunities to predict, look up documentation, and explore the code on their own allowing them to self-differentiate for the most part.
Consider having the code pre-written and commented so your students can uncomment sections as you go through them. In some cases, this will allow for them to be more available to make predictions and ask questions. Students may also require this as per an IEP, for example.
There are a lot of new vocabulary words used in this section. Consider having these words defined for students as supplemental material, on the board, large post-it, or in other easily accessible locations.
Resources
Repl.it- online IDE
W3 Schools** - readable documentation
MDN - a robust but sometimes less student-friendly documentation
Assessments
Formative:
During code along: use participation during predictive questions to informally asses whether students are following or thinking critically.
Activity Code can be collected and assessed as needed
Student Exploration and Wrap-Up reflection questions and shares can serve as formative assessments.
Summative:
Unit 3, Lab 3 (Upcoming Lab)
Final Project (End of Unit Project)
Do Now/Warm Up (~10 min)
After a few minutes, open the
index.html
and connect thescript.js
:<script src="script.js"></script>
. Open thescript.js
, comment out all lines but the first one, and ask groups for their predictions as you show each result one at a time. The answers should be: True, True, False, False, False.Don't spend much time explaining why the results are what they are. Instead, use curiosity to lead into the lesson.
Tell students that there is something weird happening behind the scenes here and that in this lesson we're going to dive deeper and find out what.
Mini Lesson: Primitive Types and Mutability (~25 min)
Primitive Datatypes vs Objects
Tell students that we will do some more analysis and predictions as we uncover what is happening. Copy down the following code and give students a minute to analyze and predict the output before taking predictions:
"numX 10" "numY 15" It may help to break down the statements one line at a time if this is not clear to your students.
Now, copy down the following code and give students a minute again to analyze and predict the output before taking predictions:
After the reveal, tell students that array Y is not being assigned to the value of the array,
[1, 2, 3]
, as they may try to intuit. Instead, the memory location of that value is saved because it's an object.Explain to students that in the first example,
10
is a primitive datatype, a single, simple value that gets passed as such. Other primitive datatypes are strings, booleans, null, and undefined. In the second example,[1, 2, 3]
is an array. Because it isn't primitive, the variablearrX
points to the specific array's memory location which gets passed toarrY
. Since they both point to the same array, when you change 1, you change the other!
The Spread Operator
What's the solution!? How do we make a copy of the array without connecting them together? We use the spread operator. Consider the following:
We've spread out the contents of arrA into a new array. Hurray! This works with objects too.
Have students take a look at this next example using the spread operator and predict what the output may be for
objA
andobjB
.The breed of
objB
changed, but the breed ofobjA
stayed the same. Without the spread operator, we would have also changed the breed ofobjA
, most likely unintentionally. With the spread operator, we can copy all the contents making a shallow copy.(Optional) The previous action of creating a copy and changing a value can be done all at once, too:
And, you can add new key-value pairs while your at it...
Documentation: Shallow Copy or In Place
Let's look at an example of a method we've seen before. Give students a minute to analyze the following code and ask them to predict if...
.map()
will change the values of the array and its mapping?arr1[0] = 9;
will change a value inarr2
, too?
Nope,
.map()
actually creates a new array with the changed values.Open the documentation for
.map()
with the students, and point out that it says "method creates a new array".Now, show students the following code and have them look up the documentation for
.filter()
and.sort()
. The goal is to find out if they will make a shallow copy, or if they will change the original array ("in place"). Then, predict the results based on their finding.filter() makes a copy and sort() works in place
For..of & Changing Values In Place
Have students take a look at the following code and predict what the output will be.
The values are passed but the original array is not changed. There are actually 2 ways we can change the values of the array:
Nested Deep Copy vs a Shallow Copy
Let's look at and predict one more example. In the example below, 2 things are "changed". Have students take a look and predict the output:
The old subject stays "Math", but the array gets a push. Very unintuitive! This is called a shallow copy. The contents were copied over, but students, because its value is an array, that array has to be spread too.
Instead of manually spreading all nested values, we can use a function called
structuredClone()
that does a deep copy for us, getting rid of all the "connections". Notice the following:
Student Exploration: Let
vs Const
(~10 min)
Let
vs Const
(~10 min)Have the students go back to the beginning of the notes and switch every
let
toconst
one at a time. Keep the ones that allow this and change it back tolet
if you encounter an error.[Bonus] - See if you can predict which ones will and will not work
[Bonus] - For each change, write down why they are causing an error or why they are allowing the change. HINT:
const
(short for constant) means cannot be changed. Are you changing it somehow?
Wrap-Up (~5 minutes)
If you'd like time to collect their work, this would also be a good time. If you are not using Repl.it Teams for Education, a great way to collect projects quickly is a Google Form where they submit their link as well as any reflection questions.
The "Student Exploration" above can be used as a wrap-up, writing activity to summarize the lesson. If you'd like to add more to that or if you'd like something more reflective, use the questions below:
Why might using
const
be more helpful thanlet
? When wouldlet
be more necessary?What was something that you struggled with today? How did you overcome that barrier?
Do you have any unanswered questions?
Last updated