string 👉🏿
In any computer programming language, a string is a sequence of characters used to represent text.
let str1 = "Hello"; // Double quotes let str2 = 'Salut'; // Single quotes let str3 = `${str1}! ${str2}!, Ca va?` // template literals* with backtick quotes console.log(str3);
*Template literals:
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
Strings reference 👉🏿
String Methods we have encountered:
let str = "SE5 8UF UK"; console.log(str.toLowerCase()); // returns 'se5 8uf uk' console.log(str.split(' ')); // returns ['SE5', '8UF', 'UK'] console.log(str.match(/[0-9]/g)); // regex to find all digits.. returns ['5', '8']
boolean 👉🏿
A Boolean is a logical data type that can have only the values true or false
if (boolean conditional) { // execute code block } else { }
// Boolean in conditional (ternary) operator let isTrue = true; let x = isTrue ? console.log("A") : console.log("B");
Boolean values
Falsy Values 👉🏿
A falsy value is a value that is considered false when encountered in a Boolean context
7 falsy values in JavaScript:
false The keyword falsezero0n BigInt, when used as a boolean, follows the same rule as a Number. 0n is falsy."", '', `` (it has a length of zero)Truthy 👉🏿 A truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy as above.
number 👉🏿
Number is a numeric data type in the double-precision 64-bit floating point format (IEEE 754). In other programming languages different numeric types can exist, for examples: Integers, Floats, Doubles, or Bignums.
Number reference 👉🏿
bigint 👉🏿
BigInt is a numeric data type that can represent integers in the arbitrary precision format. In other programming languages different numeric types can exist, for examples: Integers, Floats, Doubles, or Bignums.
A BigInt is created by appending n to the end of an integer literal — 10n — or by calling the function BigInt().
let x = 10n; typeof x; // 'bigint'
null 👉🏿
A null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address
symbol 👉🏿
A symbol value is created by invoking the function Symbol, which dynamically produces an anonymous, unique value. A symbol may be used as an object property.
Symbols unique id
let id1 = Symbol("id"); let id2 = Symbol("id"); console.log(id1 === id2); // returns false even though the two ids have the same description
An object is a collection of related data and/or functionality (which usually consists of several variables and functions — which are called properties and methods when they are inside objects.)
Class: A Key Part of Object Oriented (OOP) JavaScript
A class is a template for the creation of objects. from p5.js reference
Constructors 👉🏿 are a special method for creating and initializing an object created within a class.
Game Sketch using functions to construct objects
Game Sketch using class to construct objects
Functions 👉🏿
The Function constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and […] performance issues.
Every JavaScript function is actually a Function object.
Aside from function object, we are used to function statements/declarations
function draw() { // Place code here }
In JS functions can also be expressions
let myFun = function() { console.log('Hello'); } myFun()
Function expressions can also be created using Arrow Functions
let myFun = () => { console.log('Hello'); } myFun();
Arrays 👉🏿
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed.
Some array methods we have encountered
let arr = ['The', 'quick', 'brown', 'fox']; console.log(arr); // ['The', 'quick', 'brown', 'fox'] arr.push("jumped"); console.log(arr); // ['The', 'quick', 'brown', 'fox', 'jumped'] console.log(arr.sort()); // ['The', 'brown', 'fox', 'jumped', 'quick'] console.log(arr.join(" ")); // 'The brown fox jumped quick'
Some other array methods:
Revisiting the Airport Data Visualization from Weeks 9 and 10
const arr = [ "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Angola", "Angola", "Angola", "Angola", "Angola", "Angola", "Angola", "Benin", "Botswana", "Botswana", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cameroon", "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", "Central African Republic", "Chad", "Comoros", "Comoros", "Comoros", "Congo", "Congo", "Cote d'Ivoire", "Democratic Republic of Congo", "Democratic Republic of Congo", "Democratic Republic of Congo", "Democratic Republic of Congo", "Democratic Republic of Congo", "Democratic Republic of Congo", "Djibouti", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Equitorial Guinea", "Equitorial Guinea", "Eritrea", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Gabon", "Gabon", "Gambia", "Ghana", "Ghana", "Ghana", "Ghana", "Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Lesotho", "Liberia", "Libya", "Libya", "Libya", "Libya", "Libya", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Madagascar", "Malawi", "Malawi", "Mali", "Mauritania", "Mauritania", "Mauritius", "Mauritius", "Morocco", "Morocco", "Morocco", "Morocco", "Morocco", "Morocco", "Morocco", "Morocco", "Morocco", "Morocco", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique", "Namibia", "Namibia", "Namibia", "Namibia", "Niger", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Rwanda", "Sao Tome and Principe", "Sao Tome and Principe", "Senegal", "Senegal", "Seychelles", "Seychelles", "Sierra Leone", "Somalia", "Somalia", "Somalia", "Somalia", "Somalia", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Africa", "South Sudan", "Sudan", "Sudan", "Sudan", "Sudan", "Swaziland", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Tanzania", "Togo", "Tunisia", "Tunisia", "Tunisia", "Tunisia", "Tunisia", "Tunisia", "Uganda", "Uganda", "Western Sahara", "Western Sahara", "Zambia", "Zambia", "Zambia", "Zambia", "Zambia", "Zimbabwe", "Zimbabwe", "Zimbabwe" ] // Using reduce function to get occurrences const reduction = (acc, curr) => { // acummulator or prevVal, currentValue // if value is new, then count = 1, else (value is repeated) add 1 to count !acc[curr] ? acc[curr] = 1 : acc[curr] += 1; return acc; }; // Send results of reduction to object {} const newarr = arr.reduce(reduction, {}); // Accessing object properties with [] console.log(newarr['Algeria']); // Accessing object properties with dot syntax console.log(newarr.Algeria); // Using for...of loop* for (const key of Object.keys(newarr)) { console.log(`${key} has ${newarr[key]} airports`); } // for...in loops* for (const val in Object.values(newarr)){ console.log(val); // Return index numbers from 0 - 54 }
*For…of and *For…in loops:
Numbers
The for…in 👉🏿 statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.
Values
The for…of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.
The Document Object Model (DOM) is the data representation of the objects that comprise the structure and content of a document on the web.
The DOM represents a document with a logical tree. Each branch of the tree ends in a node, and each node contains objects. DOM methods allow programmatic access to the tree; with them you can change the document’s structure, style, or content.

<!DOCTYPE HTML> <html> <head> <!-- We would place the 'settings' for the computer to read here --> <title>Test 1 2</title> </head> <body> <h1>Saying Hello</h1> <p id="myid"></p> <script> let str = "Hello "; let elem = document.getElementById("myid"); elem.innerHTML = str.repeat(10); elem.style.fontFamily = "Helvetica"; // Modifying style </script> </body> </html>
p5 Sketch with basic DOM manipulation
Examples of DOM Manipulation in vanilla JS
Using the MDN and p5.js references, create some simple:
10 mins:
while 👉🏿 and do while 👉🏿 loops,
10 mins:
switch statements 👉🏿,
// p5.sketch to move circle up and down with switch statement let y = 200; function setup() { createCanvas(400, 400); ellipseMode(CENTER); } function draw() { background(220); if(y >= height) y = height; if(y <= 0) y = 0; ellipse(200, y, 50); if (keyIsPressed) { switch(keyCode) { case UP_ARROW: y -= 4; break; case DOWN_ARROW: y += 4; break; default: y = 200; break; } } }
45 mins:
Read Chapter 10 from Getting Started with Processing by Lauren McCarthy and complete the exercise
If there is still time left: Read Chapter 13 section ‘p5.dom’Getting Started with Processing by Lauren McCarthy and:
- create a simple sketch manipulating the DOM with p5js. - create another file manipulating the DOM using some of the common vanilla JS DOM APIS. For example:
document.getElementById(id) document.getElementsByTagName(name) document.createElement(name) parentNode.appendChild(node) element.innerHTML element.style.left element.setAttribute() element.getAttribute() element.addEventListener() window.content window.onload window.scrollTo()