1 of 34

JavaScript Primer

CS 396: �Introduction to Web Development

Winter, 2022

2 of 34

Announcements

  • Really impressed with the quality of the HW submissions and the discussion on Campuswire. Keep up the great work!
  • Lab 5 solutions posted
  • Regrade request form posted (also on the syllabus)
  • HW3 due tomorrow night
  • HW4 will be posted tomorrow, due next Tuesday

3 of 34

A Note on Heroku

4 of 34

1. Client requests a resource from a specified route

Your Flask App�port: 5000

Database Server

port: 5432

2. Python does some validation checks, then issues SQL query to request data (using TCP port; often 5432)

3. Database receives request; performs requested operation

5. Python transforms the data as JSON and sends it back to the client.

4. Database sends a response back to the caller

  • Web Browser
  • Python Tests
  • Postman

Your Laptop

5 of 34

1. Client requests a resource from a specific route on a publicly accessible web server

Flask App in Heroku�port: 80

Database Server on AWS

port: 5432

2. Python does some validation checks, then issues SQL query to another box (using TCP port; often 5432)

3. Database receives request; performs requested operation

5. Python transforms the data as JSON and sends it back to the client.

4. Database sends a response back to the caller

  • Web Browser
  • Python Tests
  • Postman

With Heroku

6 of 34

What is Heroku?

Heroku is a Cloud Platform that allows you to deploy applications in the cloud, using “containers.” The basic process:

  1. Connect your Heroku App to a repo (only deploys what’s on the main branch).
  2. Specify Python dependencies to install via requirements.txt – needs to be in the root of your repo.
  3. Specify the kind of app you’re creating via the Procfile – needs to be in the root of your repo.
  4. Install other Linux dependencies via Aptfile (N/a for photo-app).
  5. Configure environment variables.

7 of 34

Setting up your DB

  • For the photo-app, you also have to set up a cloud DB, and then ensure that your Heroku app has access to your database connection string.
    1. Heroku Postgres
  • Note that the populate command is slower over the network. Also, having multiple database commits is expensive.

8 of 34

What is a container?

Containers allow you to create self-contained apps that interact with the “host computer’s” operating system.

Heroku’s containers are called “dynos”

“ Dynos are isolated, virtualized Linux containers that are designed to execute code based on a user-specified command”

https://www.heroku.com/dynos

9 of 34

Quick Demo

10 of 34

Outline

  1. Data Types
  2. Variables
  3. Conditionals; logical and comparison operators
  4. List iteration & comprehension
  5. Practice

11 of 34

Outline

  • Data Types
  • Variables
  • Conditionals; logical and comparison operators
  • List iteration & comprehension
  • Practice

12 of 34

JavaScript Data Types

There are 7 basic data types in JavaScript:

12

number

For numbers of any kind: integer or floating-point

1.4, 33, 99999999

string

For strings (text). A string may have one or more characters, there’s no separate single-character type

‘hello world!’

boolean

for true/false.

true, false

null

for unknown values – has a single value null

null

undefined

for unassigned values – has a single value undefined

undefined

object

for more complex data structures.

{ name: 'ralph', species: 'dog' }

symbol

for unique identifiers

13 of 34

Figuring out what data type you have

typeof 'hello world!';

typeof true;

typeof false;

typeof null;

typeof undefined;

typeof 23.4;

typeof [1, 3, 4, 6];

typeof { name: 'Lester', species: 'dog', age: 15};�

See 01_datatypes.js

13

14 of 34

Objects

Objects are collections of data and functionality. There are several ways to create them (see 01_datatypes.js for additional examples). Here is one way:

const person = {

firstName: "John",

lastName: "Doe",

age: 50,

getFullName: function () {

return this.firstName + " " + this.lastName;

}

};

console.log(person.getFullName(), person.age);

15 of 34

Functions that convert between data types

// String(), Number(), Boolean()

console.log(123, typeof 123, String(123), typeof String(123));

console.log('123', typeof '123', Number('123'), typeof Number('123'));

console.log('true', typeof 'true', Boolean('true'), typeof Boolean('true'));

See 01_datatypes.js

15

16 of 34

Outline

  • Data Types
  • Variables
  • Conditionals; logical and comparison operators
  • List iteration & comprehension
  • Practice

17 of 34

Naming Variables: Case Sensitivity

JavaScript is case-sensitive.

let a = 3;

let A = 33;

console.log(a);

console.log(A);

17

18 of 34

Naming Variables

Although JavaScript doesn’t care what you name your variables (beyond the rules specified in the previous slide), there are some conventions that most people follow:�

  1. “camelCase” for functions and variable names
    • firstName = 'Beyonce';
    • lastName = 'Knowles';
  2. Hyphens for file names: hello-world.js

18

19 of 34

Declaration keywords

In JavaScript, there are three keywords for declaring variables: let, const, and var

  • let – Means that the variable may be reassigned. It also signals that the variable will be used only in the block it's defined in (we’ll talk about this more when we get to functions).
  • const – Means that the variable won't be reassigned. It’s immutable. Used in cases where you declare and assign once, but don’t change it afterwards.
  • varAvoid. Old way to do things prior to ES6.�“The weakest signal available” — “The variable may or may not be reassigned, and the variable may or may not be used for an entire function.”

19

20 of 34

const and let demo (console)

02_variables.js

20

21 of 34

Outline

  • Data Types
  • Variables
  • Conditionals; logical and comparison operators
  • List iteration & comprehension
  • Practice

22 of 34

If / Else Statement

22

if (a === 5) {

console.log('a is 5');

} else if (a > 5) {

console.log('a greater than 5');

} else {

console.log('a is less than 5');

}

23 of 34

Comparison Operators

= Assignment

== Equality

=== Strict Equality (both values and data types match)

!=, !== Not Equal

>, >= Greater than; greater than or equal to

<, <= Less than; Less than or equal to

03_operators.js

23

24 of 34

Operator Quiz

const num1 = "50";

const num2 = 50;

Will this statement evaluate to true or false?

num1 == num2;

25 of 34

Operator Quiz

const num1 = "50";

const num2 = 50;

Will this statement evaluate to true or false?

num1 === num2;

26 of 34

I. OPERATOR QUIZ

const num1 = "50";

const num2 = 50;

What will be stored in the “result” variable?

const result = num1 + num2;

27 of 34

Logical Operators

&& AND

|| OR

! NOT

27

28 of 34

Outline

  • Data Types
  • Variables
  • Conditionals; logical and comparison operators
  • List iteration & comprehension
  • Practice
  • [if time] Objects & prototypal inheritance

29 of 34

Iteration and Looping

29

30 of 34

Iteration and Looping

  1. “for” and “while” loops (the classics)
  2. for...of loop (to access each item in a list, sequentially)
  3. Built-in list functions (forEach, map, filter, and reduce)

30

05_loops_and_iteration.js

31 of 34

1. For and While loops

The classics...

// while loop�let i = 0;�while (i < 5) {� console.log('The counter is:', i);� i++;�}

// for loop�for (let i = 0; i < 5; i++) {� console.log('The counter is:', i);�}

31

05_loops_and_iteration.js

32 of 34

2. For .. Of Syntax

32

for (const item of array) {

// do something� // with item

...

}

For item in array�assigns each item in the array to a variable name of your choice

BLOCK�If the statements that will repeat for each item in the array

05_loops_and_iteration.js

33 of 34

3. forEach, map, filter, and reduce

See demo!

05_loops_and_iteration.js

34 of 34

If time...

34