Reference and Resources
Written by Neel Kishnani, Jason Chuen, Michael, and the teaching team
Here is a collection of some of the major syntax, elements, functions, etc. we use in this course. What is mentioned below follows best practices and our recommendations; the lecture materials may discuss alternatives.
Links point to MDN Web Docs.
HTML
General Page Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
...
</head>
<body>
...
</body>
</html>
Page Regions
<header> |
Header of hte page, or of a section/article/region |
<main> |
The main content of the page (e.g. the part that's not part of a nav bar or footer) |
<footer> |
Footer of the page or a region |
<nav> |
A collection of links/buttons/etc. for moving around the page/app |
<section> |
A section of the page (e.g. a sidebar, block of content with a heading, etc.) |
<article> |
A section of hte page that is more independent, e.g. a blog post, announcement, event, etc. |
Elements for User Interaction
<form> |
Wrap a collection of controls (inputs, buttons, etc.) |
<input> (leaf) |
Accept user input (see type ) |
<label> |
Associate a label with an input (wrap <input> or use for attribute) |
<button> |
A clickable button. Default type is submit if inside a form |
Note: Elements marked "(leaf)" don't contain children and thus should not have a closing tag.
CSS
Selectors
tag |
Select all <tag> elements |
.class |
Select all elements with class class |
#id |
Select all elements with id id |
type.class |
Select type elements with class class |
s1 s2 |
Select s2 if s2 is a descendant of s1 |
s1, s2 |
Select s1 and s2 |
s1 > s2 |
For direct child |
.box > * |
For universal selector (ex. selects all direct children of .box) |
Font and Color Properties
Positioning
static |
(default) element is positioned according to the normal flow of the document |
relative |
positioned relative to its normal position |
absolute |
elements are removed from the normal flow |
fixed |
element is positioned relative to the viewport |
Layout Properties
display |
Override default page flow with block or inline |
Spacing and Box Model
margin |
Add spacing between elements outside of the border. (Can set separately like margin-top |
padding |
Add spacing to an element inside the border. |
Calculations
calc() |
For performing calculations |
Units/Size
percentage |
Represents a percentage value |
viewport |
For Browser Window |
em |
For relative unit (emphasizes text) |
rem |
For font size of root element |
height |
For setting height |
width |
For setting width |
JavaScript Language
Syntax
+, -, *, /, % , etc. |
Arithmetic, comparison, assignment, and other operators |
=== and !== |
Equality tests (don't use == and != except with null ) |
let x = ...; |
Declare variables |
const x = ...; |
Declare constants (can't be reassigned) |
let arr = [...]; |
Declare and initialize an array |
let obj = { key: value, ... }; |
Declare and initialize an Object (key/value store) |
arr[i] , obj[x] |
Get element/value from array/object (undefined if out of bounds/key doesn't exist) |
obj.x |
Get value of "x" (the literal key x, not the variable) in obj |
"key" in obj |
Check if key exists in obj |
delete obj.key; |
Remove key/value pair from obj. (Note: does not work for arrays) |
Data Types
Boolean |
true or false |
Number |
Integer or floating-point (real) numbers |
String |
Unicode strings (suggest double quotes for literals) |
null |
"Intentionally" not set |
undefined |
Not set (uninitialized, out of bounds, etc.) |
Array |
Indexed collection of elements |
Object |
Collection of key/value pairs |
Error |
Represents an exception (that can be thrown), with a message and stack trace |
Object methods (called on Object itself)
Async Logic
async function |
Declares an async function where the await keyword is permitted within the function body. |
await |
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async function or at the top level of a module. |
JavaScript in the Browser
Global variables and functions
console.log(...) |
Print to browser console |
alert(message) |
Display an alert message (popup). Good for quick/testing messages |
window |
"Global object": Store variables here to access them in console |
document |
Access parts of the DOM and DOM manipulation functions |
fetch |
the fetch method provides an easy, logical way to fetch resources asynchronously across the network. |
Manipulating DOM Elements
elem.id , elem.href , elem.src , etc. |
Get/set HTML attributes on an element |
elem.textContent |
Get/set text (string) inside element. Setting to "" also removes all children |
elem.style |
Get/set CSS properties of an element. Properties use camelCase instead of hyphens (e.g. fontSize for CSS's font-size ). |
elem.classList |
Get/modify list of CSS classes of an element |
Databases and MongoDB
Query Operators
$gt |
Matches values that are greater than a specified value; db.groceries.find({cost: {$gt: 30} }) |
$lt |
Matches values that are less than a specified value; db.groceries.find({cost: {$lt: 100} }) |
$gte |
Matches values that are greater than or equal to a specified value; db.groceries.find({cost: {$gte: 50} }) |
$lte |
Matches values that are less than or equal to a specified value; db.groceries.find({cost: {$lte: 200} }) |
$in |
Matches element in array; db.students.find({ id: {$in: ["kashif", "michael"]} }) |
$regex |
Matches text (regular expression); db.courses.find({ code: {$regex: "^CS106"} }) |
Update Operators
$set |
replaces the value of a field with the specified value |
Accessing a collection
conn.db(name) |
Get a database object (not async) |
db.collection(name) |
Get collection object (not async) |