reading-notes

Programming with JavaScript

MDN Control Flow

JavaScript Functions

JavaScript Operators

Expressions & Operators - Web Docs

Functions - Web Docs

Control Flow

if(isEmpty(field)) {
  promptUser();
} else {
  submitForm();
}

JavaScript Functions

Why Functions?

JS Function Syntax

function name(parameter1, parameter2, parameter3) {
  // code to be executed
}

Function Return

let x = myFunction(4, 3);   // Function is called, return value will end up in x

function myFunction(a, b) {
  return a * b;             // Function returns the product of a and b
}

The result in x wil be:

12

Global vs. Local Variables

Global Variables

let carName = "Ford"
// code here CAN use carName

function myFunction() {
  return "I love my " + carName
// code here CAN use carName
}
// code here CAN use carName

Local Variables

// code here can NOT use carName

function myFunction() {
  let carName = "Volvo";
  // code here CAN use carName
}

// code here can NOT use carName