This is an outline for teaching an introduction to JavaScript at NICAR 2022.
- What is JavaScript and why is it important?
- Learn JS fundamentals by running code in the browser
- types
- numbers
- variables
- diy function and use console.log
- arrays
- loops
- objects
- booleans (time permitting)
- conditionals (time permitting)
- Interacting with the DOM
- what is the DOM
- write to the DOM
- event listener for button click
- render to the DOM based on a button click
- loop through an array of objects and render them (time permitting)
- HTML?
- CSS?
- JavaScript?
- Any other programming lanuages?
JavaScript is one of the most popular programming languages. It's particularly popular because it allows you to run code on web pages, like in your Chrome or Firefox or Safari web browser.
For creating web pages, you have a trinity:
- HTML: What it is. Concerned with the structure of your webpage and what's on it. It has elements like a heading or a paragraph.
- CSS: What it looks like. Concerned with appearance. Styles the HTML elements to look good.
- JavaScript: What it does. Concerned with the user interactions and behavior of the page. It can also change the HTML and CSS. It's often used to make maps and data visualations.
(TODO, get some examples)
Explain that javascript doesn't have to be used on the front-end, it can also be used on the back-end, but that is outside of the scope of this session.
We can demonstrate how to run code in your browser. Through my demonstrations and coding alongside of me, you’ll learn about some of the basic types of JavaScript.
A type is a data structure in JavaScript. There are a lot of types. You can think of the different types as having different abilities to do things within a program. Types are universal across programming languages, but they might go by different names. We'll learn several types throughout this session, and hopefully through learning the different types I'll better define what a type is.
A number is an example of a type. A number is what you think it is. You can do math operations on numbers. Numbers are universal across programming languages.
- open console in the
types/index.html
page. - demonstrate how you can run code in the console:
5 + 5
16 - 32
3 * 2
16 / 4
- Together, we're going to work in the console. Every web page has this and it allows you to test and run code in the browser. Clearly tell everybody to open their console. Show how you can do this. Make sure everybody is on the same page, with their console open, before proceeding.
- together, we'll add:
- add: how would you add?
- subtract: how would you subtract?
- multiply: how would you multiply?
- divide: how would you divide?
- write your own add operation.
- multiply three numbers together. it works like a calculator.
A variable is a special way of storing values. For example, you can use a variable to store a number, or even the numerical result of a math operation. Variables are universal across programming languages.
let thisYear = 2022
- Add two numbers together and set it to a variable:
let nextYear = thisYear + 1
- Demonstrate how you can add numbers with variables.
- Note that you can style the variable to whatever you want and it will still work, but camelCase is a common convention in javascript.
- Make sure everybody understands variable. Make sure everybody is in their console.
- instruct everybody to create a
thisYear
variable. - subtract one from
thisYear
and store the result in a variable calledlastYear
- multiply two numbers together and set it to a variable
- add a number to the multiplication variable
- using the
thisYear
variable we defined, subtract 2 from it and store the results in a variable. call the variable whatever you want, maybe something likepandemicStartYear
.
A function is a fundamental building block in javascript. It's a set of procedures that performs a task, like calculating a bunch of numbers. It can take an input, and return an output. I can write my own functions, but there are also a lot of built-in functions in javascript. Here we will do both.
And now, instead of the console, we're gonna write this code in the javascript file, save it, and reload the page.
- I can write my own function called add:
function add(num1, num2) {
let result = num1 + num2
return result
}
- note that the spaces and new lines don't matter here. what's important is the curly brackets, which is the function body, and holds all of the instructions/code. the curly brackets are what's known as syntax.
- can "call" this function:
add(2, 2)
- set the result to an
addTwoNumbers
variable. - in order to see the results, since this is in the javascript file and not the console, we gotta console.log it. console.log is a function that takes arguments and prints the argument you pass into the function.
- to reiterate, we're writing this code in a javascript file, saving the file, and reloading the page. make sure everybody is in the same file before proceeding. do it in the types/a.js
- We can write a function together called multiply:
function multiply(num1, num2) {
let result = num1 * num2
return result
}
- what do you think this code will do if you pass in 2 and 5?
- set the result to a variable:
let multiplyTwoNumbers = multiply(5, 5)
? - console.log. this is a new concept, so make sure everybody does this before proceeding.
- write a function called
subtract
. it should take in two parameters. call the subtract function and set it to a variable. console.log the results, and make sure the results are logged in your console. Functions are universal across programming langues.
We're gonna learn a new data type called a string. a string is used to represent and manipulate a sequence of characters. basically anything you can type on your keyboard can be a string. Strings are universal across programming lanuges.
- create a variable called
name
and set that value as my name - show how you can call the variable in the console
- create a variable called
hometown
and log it - combine hometown + name and set it to a variable called greeting
- console.log greeting
- note how you need to add some spaces to the strings so that you can read it and the words don't run together.
- do it in types/b.js. change the script tag in your html. make sure everybody is on the same page before proceeding.
- do the I do with everybody. i'm gonna delete all of my code and we'll do it together.
- building upon the greeting string. add you own string to it. you can do something like age or favorite book. (TODO: come up with better exercise!)
So if we have these variables name and hometown, it would make sense to keep them together a single entity. You can do that with what's called an object. An object is a collection of key/value pairs. Objects are universal in programming languages but go by different names.
- create an object named bio with the keys name and hometown.
- console.log these keys
- create a greeting with the key
- do the above example with everybody
- add a new key to your bio object. something like age or favoriteBook
- create a greeting with all of the keys from your object.
- console.log the greeting
An array is a list of items. The items can be strings, numbers, objects, etc. You can access a single item in the array based on the index where it exists in the array. There are a lot of built-in operations you can do with an array. Arrays are universal in programming languages.
We're gonna work in a new file for this. Make sure to change the refence in the HTML.
- create an array called pets
- access the array by index.
- note that the index starts at 0 instead of 1. it's called zero-based indexing.
- console.log the item in the array
- We're gonna work in a new file for this. Make sure to change the refence in the HTML. Make sure everybody is in the same file before proceeding.
- create an array called southernCities
- access the array by index
- console.log the item in the array
- create an array called friends.
- add like 2-3 of your friends. don't go overboard because of time constraints!
- access an item in the array by index.
- console.log the item
A loop is a quick and easy way to do something repeatedly. They're often used with arrays. Loops are universal in programming.
We're gonna work in the same file as the array example.
- using the array called pets that I already defined, loop through each one
- i'm using the forEach function. built-in to JS to loop over arrays
- be sure to explain the syntax
- console.log each pet
- using forEach, we're gonna loop over each southernCity and console.log it
- make sure everybody has it working before proceeding.
- the syntax can be tricky.
- using forEach, loop over your friends array and console.log each friend
A boolean is a true/false value.
We're gonna write the code in our console for this.
- 5 > 6: returns false
- 5 === 5: returns true
- 6 > 5: returns true
- We're gonna write the code in our console for this. Make sure everybody is doing this.
- continue to write in the console.
A conditional is an if/else statement in the most basic sense. It's a way to control the flow of your program, and only do things if certain conditions are met. Conditionals are built on booleans. If this is true, do this; else, do that. they can be as simple as just an if statement, or if/else, or if, else if, else, etc
Continue using the array file.
- create a boolean based on math
- show how booleans don't have to be math.
- grab an item in the array and use that for the conditional.
- use an index that doesn't exist in the array and show how that's "false"
- create a boolean based on math
- show how booleans don't have to be math.
- grab an item in the array and use that for the conditional.
- use an index that doesn't exist in the array and show how that's "false"
- create your own boolean with a math statement. console log it
Now the fun stuff. We're gonna use javascript to change what shows up on our web page, and use javascript to make a button click control what shows up.
The DOM is short for the Document Object Model. It represents the HTML on your webpage. Using JavaScript, you can do things with the DOM, like add your own HTML, or listen to button clicks, or change the CSS, etc.
- working in a new script called
dom/hey.js
, with a new index.html calleddom/index.html
. - In our
dom/index.html
file, we have a<div>
element with an id oftext
. The id lets us grab the div element and write some HTML instead of the div. - take the id and write the code that adds it to the DOM:
let textDiv = document.getElementById('text')
textDiv.innerHTML = "i put some text here, within the div"
- working in a new script called
dom/hey.js
, with a new index.html calleddom/index.html
. Make sure everybody sees the same web page (with the button) and is in the same hey.js file before proceeding!! - change the id from
text
tohi
- this lets us target that html element in our JS based on the id
- in our js file, write the code to add a message to our page
- make sure to do camelCase for getElementById!!! else it won't work.
let name = 'sam'
let textDiv = document.getElementById('hi')
textDiv.innerHTML = 'hi NICAR! my name is ' + name
- in your html create a new div, with a new id
- target that id the document.getElementById function and set it to a variable
- use that variable to set the
innerHTML
with a string. add whatever you want to the string
- i'm gonna add a button where i can click and make that show up
- first, i gotta move that code into a function called sayHi
- demonstrate how, with that code change, i'm not changing what's on the page
- call the function within the script
- then, reference the function with the click. depending on time, do this directly on the button, or go ahead and use the document.getElementById('btn').addEventListener('click' sayHi)
- do this previous step all together
- encapsulate the other code you wrote alone into a function
- instead of calling sayHi, call that function in your event listener.
- do conditionals and booleans
- refactor the DOM code to append child...
- get the georgiaWriter's data and render that on the screen