<info 340/>

More JavaScript Syntax

Joel Ross
Autumn 2026 (A)

View of the Day

Goal today is to mostly wrap up JavaScript and be ready to start React on Wed!
 

  • Interactive JavaScript review (demo)

  • Arrow Functions (lecture + pollev)

  • Destructuring (lecture + pollev) 

  • ES6 Modules (demo, if time)

 

Today is a syntax day (again)!

Q&A

Event Handling

Use the addEventListener() function to register a "listener". The function takes 2 arguments: the type of event to listen for (a string), and a callback function to run when the event occurs.

the event that occurred

const button = document.querySelector('button');

button.addEventListener('click', function(event) {

  //what to do when button is pressed!
  console.log("you clicked on", event.target);  

});

which element

cause the event

JS Web App Structure

To develop a web app, think about your page as "displaying dynamic content", not "responding to user actions".

  1. Define the data (state) that your app will be displaying
    • ex: the list of tasks, the current recipe, the user's history
       
  2. Define how to render (display) that data using functions
    • ex: createTaskItem(), renderRecipe() 
       
  3. Define responses to user action. When the user does something:
    1. Update the state data
    2. "re-render" the (now-changed) data!

JS Web App Structure

//define data
const state = {
  data: [ {}, {}, {} ],
  ...
}

//define presentation - lots of these kinds of functions
function renderData() {
  //render all the data
  for(let datum of state.data){
    //call helper functions to break things up!
    const dataCardElement = renderDataCard(datum);
    document.querySelector('#main').appendChild(dataCardElement);
  }
}

//define user interaction
button.addEventListener('click', function(event) {
  state.data[i] = ...; //MODIFY THE STATE
  document.querySelector('#main').innerHTML = ''; //CLEAR OLD VIEW
  renderData(); //RE-RENDER CONTENT    
})

renderData() //show initial content!

Syntactic Sugar causes cancer of the semicolon

Arrow Functions

A shortcut syntax for declaring functions using =>

//normal function declaration
const foo = function(params) {
  return 'foo '+params;
}
//arrow function declaration (block body)
const foo = (params) => {
  return 'foo '+params;
}
//arrow function declaration (concise body)
const foo = (params) => 'foo '+params;

single expression implies return

//arrow function declaration (concise, no params)
const sayHello = () => console.log('Hello world!');

anonymous function assigned to a variable

Arrow Functions

Arrow functions are particularly nice for anonymous callback functions (literals), and in fact should always be used.

const array = [1,2,3]; //an array to work with

//normal function
array.map(function(num) {
  return num*2; //multiply each item by 2
});

//arrow syntax
array.map((num) => {
  return num*2; //multiply each item by 2
});

//concise body - ONLY FOR TINY EXPRESSIONS
//(better to avoid entirely)
array.map((num) => num*2);

const doggy = {
  name: "Fido",
  bark: () => console.log("woof")
}

Check your understanding

/* function declaration */
function myFunction(param1, param2) {
  return param1 + param2;
}

/* function literal */
const myFunction = function(param1, param2) {
  return param1 + param2;
}

/* arrow function */
const myFunction = (param1, param2) => {
  return param1 + param2;
}

/* arrow function (concise body) */
const myFunction = (param1, param2) => param1 + param2;

We now have 4 different ways to write functions. They all have different syntax, but do the same work.

Function Syntax

What do you think?

Code Style

Recommendation: don't use concise body syntax. Always include the explicit block.

  • It's easier to read (more clearly a function)
  • It's easier to modify (can add additional statements)
  • It's easier to debug (can add console logs)
/* Do this */
const exclaimed = stringArray.map((aString) => {
  return aString + "!!" //add exclamation points
})

/* Do not do this */
const exclaimed = stringArray.map((aString) => aString + "!!")

Destructuring

Destructuring assignment lets you assign (unpack) each value of an array or object into into separate variables in a single operation.

//destructuring arrays
const myArray = [1, 2, 3];
const [x, y, z] = myArray; //myArray elements to `x`, `y`, `z`
console.log(x); //=> 1;
console.log(y); //=> 2;
console.log(z); //=> 3;

//destructuring objects
const myObject = {a: 1, b: 2, c: 3};
const {a, b, c} = myObject; //myObject.a to a, etc.
console.log(a); //=> 1
console.log(b); //=> 2;
console.log(c); //=> 3;

Check your understanding

Destructuring Params

It's possible to destructure function parameters. This is commonly used in React (with object params)... but can make code maintenance harder.

//an example person object
const person = {name: 'Ada', height: 64, weight: 135}

//a function that expects a person object
function getBMI(personObj) {
  const height = person.height;
  const weight = person.weight;
  return 703*weight/(height*height);
}




const adaBMI = getBMI(person);
//implicit
const personObj = {
   name: 'Ada', height: 64, weight: 135
}
//implicit - destructured
const {height, weight} = {
   name: 'Ada', height: 64, weight: 135
}
//an example person object
const person = {name: 'Ada', height: 64, weight: 135}

//a function that expects a person object
function getBMI({height, weight}) {

  
  return 703*weight/(height*height);
}




const adaBMI = getBMI(person);
/* standard argument */
function getFullName(personObj) {
  return personObj.first + " " + personObj.last;
}

/* destructured argument */
function getFullName({first, last}) {
  return first + " " + last;
}

We now have 2 different ways to write function arguments. They all have different syntax, but do the same work.

Function Syntax

What do you think?

Spread Operator

const originalArray = ['a', 'b', 'c', 'd'];

const newArray = [...originalArray, 'e', 'f'];


console.log(newArray) //['a', 'b', 'c', 'd', 'e', 'f']

The spread operator ... lets you reference "the elements of" an array or object when declaring a new value. This is particularly useful for duplicating arrays or objects.

const person = {name: 'Ada', height: 64, weight: 135}

const copyOfPerson = {...person}; //clone an object!
console.log(person === copyOfPerson); //false! different objects!

//all off the properties are "spread" into the new object
const personWithHat = {hat: 'bowler', ...person}
console.log(person); //has name, height, weight
console.log(personWithHat); //has name, height, weight, hat

think: "the elements of original array"

ES 6 Modules

Modules are self-contained, isolated scripts that are able to run in their own namespace (without polluting the global environment). Modules can export values that may be imported and used by other modules.

 

Web pages load modules by including a script tag with the type="module" attribute.

<!-- load a script as a module -->
<script type="module" src="path/to/module.js"></script>

Live-Server

cd path/to/project
npx live-server .

where index.html is!

dot to serve entire folder

When an HTML page is opened with the file:// protocol, JavaScript cannot access system files. You need to run a local web server instead to use the http:// protocol.
You can do this with the live-server package

Access the webpage at http://localhost:8080/index.html

Stop the server by using ctrl+c in the command shell.

Importing

It is "possible" to import external libraries and make them available in your script from JavaScript (not HTML)

CommonJS (used by Node.js)

ES 6 Modules (used by browsers)

const util = require('util');

node version of "import"

global to refer
to the library

module library name

import { ArrayList } from 'util'; //named import

module library name

Java

import java.util.ArrayList;

module name

variable to import

variable to import

Exporting Values

export variables, functions, and classes to be available for other modules to import.

/*** my-module.js ***/
export function foo() { return 'foo'; } //named export

export const bar = "bar"; //export a variable

//will not be available (a "private" function)
function baz() { return 'baz'; }
/*** index.js ***/
//example of named imports:
import {foo} from './my-module.js'; //single named import
import {foo, bar} from './my-module.js'; //multiple named import
foo() //=> 'foo'
console.log(bar) //=> 'bar'

relative path to file (start with ./)
.js extension will be implied by default in Node

Importing Options

Use the as keyword to "alias" a value.

Use import * syntax to import all values from a module.

/*** my-module.js ***/
export function foo() { return 'foo'; } //"named" export

//provide an "alias" (consumer name) for value
export { bar as yourBar };
/*** index.js ***/
import { yourBar } from './my-module.js'; //import value by name
yourBar() //=> 'bar'

import { bar } from './my-module.js'; //error, no value `bar` exported

//provide "alias" for value when importing!
import {foo as myFoo} from './my-module.js';
myFoo(); //=> 'foo'

//import everything that was exported
//loads as a single object with values as properties
import * as theModule from './my-module.js'; 
theModule.foo(); //=> 'foo'
theModule.yourBar(); //=> 'bar'

Default Exports

Each module can have a single default export, which provides a shortcut when importing.

/*** my-module.js ***/
export default function sayHello() {
    return 'Hello world!';
}
/*** index.js ***/

//default import can assign own alias without needing name!
//think: "import {default as greet} from './mymodule.js'"
import greet from './my-module.js';

greet(); //=> "Hello world!"

Be careful about whether an export is named or default!

External libraries especially are not consistent.

When to export?

  • Only export a value if it must be used by another module. When in doubt, keep values private.
     
  • In general, prefer named exports. This makes it easier to export additional values later if needed.
     
  • If a module is designed around a single value or function (e.g., a single React Component), make it a default export. But a single exported value isn't necessarily a default export!

Action Items!

Calendar

  • Mon 11/02: JavaScript Syntax

  • Wed 11/04: React Syntax

  • Mon 11/09: maybe React with AI?

  • Wed 11/11: holiday

  • Mon 11/16: definitely React & Projects & AI

Action Items!

Action Items!

  • Start task list for Week 6

    • May need to catch up with videos/etc

  • Preread: Chapter 15: Introduction to React

  • Problem Set 06 due Friday

    • This one is bigger and challenging (problem-c especially)!

 

Next: React! (and ES6 modules if needed)

/*** module.js ***/
export function foo() { console.log('foo') }

export function bar() { console.log('bar') }

export default function baz() { console.log('baz') }
/*** index.js ***/

/* A */
import bar from './module.js';

/* B */
import { bar } from './module.js';

/* C */
import * as bar from './module.js';

/* D */
import { * as bar } from './module.js';

//call bar to print "bar"
bar();

info340au26a-es6

By Joel Ross

info340au26a-es6

  • 76