JavaScript essentials
Topic Slides
Agenda Slides Open fullscreen β
Open slides ↗Post-Class Notes
TL;DR#
Today started with the Mini Project 2 wish list, then four review questions about how the browser handles forms, styles and storage, then variables, types and functions in the JavaScript essentials slides. The habit worth keeping: when you are unsure what a value is, check it with typeof in the Console before you guess.
Before next class#
There will be no class on Tuesday 10/13 (fall break). Checkpoint 3 now closes Monday 10/12, one day later than announced.
- Mini Project 2. Pick one idea from your wish list. The spec's suggested pace has you write
PROPOSAL.mdandPRD.mdin a new public repo next. The project is due Tuesday 10/20, deployed and working by 3:00 PM. - Temperature Converter. Create
converter.htmlin youroim3690repo. Before you ask your agent, write down how you would writefunction toFahrenheit(celsius); then follow the steps on the slide, and commit and pushconverter.html. - Read the JavaScript essentials slides up to the converter (slide 29). In particular, read the slides we did not reach in class: Forgetting const, return vs console.log and The Order Code Runs In.
logs/wk06.mdon GitHub by Monday 10/12, with three ideas under the heading## MP2 Wish Listand your Predict Before You Delete notes. Open your repo on GitHub and check that it is there.
Which ideas fit Mini Project 2#
Mini Project 2 is a tool that runs in the browser alone: it remembers some data and tells you something from it. The data can be made up. Keep the ideas that do not fit in your list, because they have a home later:
- It needs a server, a database, an outside API or a language model (recipes from an online source, text analyzed by an AI model, millions of rows of data): Mini Project 3 or your final project.
- It reads or changes another website (TikTok, Canvas): Mini Project 4, a browser extension. A student in an earlier class built one that showed RateMyProfessor ratings next to the courses in a course listing.
Mini Project 2 also asks for design: every state of your tool should look the way your DESIGN.md says.
What View Page Source shows#
A correction: after JavaScript adds a task, View Page Source does not show it. View Page Source shows the HTML file as the browser loaded it. The task exists only in the page the browser built from that file, called the DOM, and that is what the Elements tab in DevTools shows. The review question's answer, No, only on the page, was correct.
When you debug Mini Project 2, look in Elements for what your JavaScript added, and in Application β Local storage for what it saved.
Passwords and the GET method#
A form with method="get" puts every field in the URL, so a password shows up in the address bar and stays in the browser history. With method="post" the fields go in the request body, which you can see in the Network tab. POST keeps the password out of the URL. Only HTTPS keeps it from being read on the way to the server.
Uploading a file from a form#
A form with a file input needs method="post" and enctype="multipart/form-data". Without the enctype attribute, every browser sends only the file's name. enctype stands for encoding type. It changes how the data is packaged; HTTPS is what encrypts it.
The file uploaded in class went nowhere. The course's practice server reads what a form sends, shows it back to you on the page, and saves nothing.
Why the box stayed light blue#
#box { background: lightblue; }
.on { background: yellow; }After box.classList.add("on"), the box has both rules, and the id rule wins whatever order the rules are in. Click the box in Elements, and the Styles panel shows the yellow background crossed out. background: yellow !important; overrides it.
Where local storage lives#
Tasks saved in localStorage belong to one browser on one computer. The same page in another browser, or on a classmate's laptop, starts empty. Sharing data between browsers takes a server, which is Mini Project 3.
The data stays until it is cleared, with one exception: Safari deletes a site's local storage after seven days of Safari use without any interaction with that site.
Why some values have quotes#
In your to-do's saved data, the task's text has quotes and the id and true do not. Each value has a type:
| Type | Example | Notes |
|---|---|---|
| string | "Buy milk" |
text, always in quotes |
| number | 90 |
"90" in quotes is a string |
| Boolean | true, false |
only those two values, no quotes |
| object | { id: 1, text: "Buy milk", completed: false } |
named fields, like a dictionary in Python |
| array | [1, 2, 3] |
a list; your to-do's tasks are an array of objects |
Check any value in the Console with typeof:
typeof "Buy milk" // "string"
typeof 90 // "number"
typeof true // "boolean"Declaring with const and let#
Use const for a name you will not reassign and let for one you will. If your js/todo.js declares let todos, look for a line that gives it a new list, such as todos = todos.filter(...): that line is why it needs let. If there is no such line, const would do. A const array can still have items added; you cannot point the name at a different array.
A correction: leaving out const and let is not fine. In a plain <script>, the name quietly becomes a global variable, shared with every other script on the page; in a module, the same line stops with an error. Write const or let every time (Forgetting const).
The string-number trap#
Whatever a user types into an input reaches JavaScript as a string, even when it looks like a number. A correction: check every time whether a value is a number or a string, because + joins strings and the other math operators convert them:
"20" + "10" // "2010"
"20" + 10 // "2010"
"20" - 10 // 10Convert first with Number(), as in Number(input.value), and 20 + 10 gives 30. Step 4 of the converter shows what happens when you delete that Number().
The banana from class works the same way. In ('b' + 'a' + + 'a' + 'a').toLowerCase(), the second + of the pair tries to turn 'a' into a number and gets NaN (not a number), which is then joined to the other letters as text: baNaNa, then banana. Full reference: Stack Overflow.
Functions and arrows#
A function names a set of steps so you can run them again, and its parameters let each call use different values:
function add(a, b) {
return a + b;
}
add(1, 2); // 3Your to-do uses this form and a shorter one, the arrow function. => is an arrow; greater than or equal to is written >=. What comes before the arrow is the input, and what comes after it is what the function does with it.
const add = (a, b) => a + b;Smart cities and the first cars#
After the lightning talk on Toyota's Woven City, we compared it with the first cars: Britain's Locomotive Act of 1865 required a person carrying a red flag to walk at least 60 yards ahead of any self-propelled road vehicle. For a project like a smart city, also ask who earns from building the infrastructure, whether or not people end up living there.