02-JavaScript
02-JavaScript
JavaScript
2
Basics
3
Variables
Two
▪ Three
XXXX ways to declare variables: var,
XXX let, const
(var has non-block scope "hoisting", generally you don't want that)
▪ Primitive types:
- boolean, number, string, null, undefined
- everything* else is an object, including arrays
4
Truthy and Falsy
https://www.sitepoint.com/javascript-truthy-falsy/ 5
Logical Or and "Nullish Coalescing"
6
Functions
▪ Function declaration
function add1(a, b) { return a + b; }
console.log(add1(1, 2)); // 3
▪ Function expression
const add2 = function (a, b) { return a + b; }
console.log(add2(1, 2)); // 3
7
First Class Functions
8
Closures
9
Passing Functions to Factory Functions
10
String Template Literals
▪ Example
const v = 15.7;
const units = "cm";
▪ Without string interpolation:
let msg = "Length is " + v + " " + units + ".";
▪ With string interpolation:
let msg =`Length is ${v} ${units}.`
▪ Can use expressions in template literal:
let msg =`Length is ${(v / 100).toFixed(2) * 100} cm.`
formatting method of
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals primitive type 11
JavaScript Objects
▪ Get property
console.log(square.colour); // red
▪ Set property
square.colour = "blue";
* we don't need to quote property names and some values, plus function property, ... 12
Prototypal Inheritance
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain 13
Prototype Chain using Constructor Function
// a constructor function
function Shape(colour) {
"this" refers to object context
this.colour = colour;
this.draw = function () {
return `A ${this.colour} shape.`;
}
}
class Shape {
constructor(colour) { this.colour = colour; }
draw() { return `A ${this.colour} shape.`; }
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_classes 15
Arrays
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections 16
Array Methods
▪ foreach
▪ sort
▪ reverse
▪ splice
▪ indexOf
... many more
Note some mutate the array and some don't, so check the docs!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 17
Common Functional Array Methods
18
Destructuring Assignment
▪ From Objects
let obj = { "a": 1, "b": 2, "c": 3 };
let { a, b } = obj; // a = 1, b = 2
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment 19
Spread Syntax and Rest Syntax
const obj = { a: 1, b: 2, c: 3 };
let { a, ...x } = obj;
console.log(x); // {b: 2, c: 3}
20
Create a Prepopulated Array using spread and map
21
Resources for Learning JavaScript
▪ Strings
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
22
Exercise
EXERCISE
1. Create a Vite Vanilla JavaScript project
- npm create vite, then answer prompts and follow instructions
- Drag folder into VS Code
Web Apps 23