reading-notes

Class 7 Notes

Control Flow

Source: https://developer.mozilla.org/en-US/docs/Glossary/Control_flow


JS Functions

Source: https://www.w3schools.com/js/js_functions.asp

Example: ‘ // Function to compute the product of p1 and p2 function myFunction(p1, p2) { return p1 * p2; }’

Function Syntax

Example: ‘ function name (parameter1, parameter2, parameter3) { // code to be executed }’

Function Invocation

Function Return

Example: ‘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 this case is 12

Why Functions

  1. It allows you to reuse code - define it once and use it many times
  2. It allows you to use the same code many times with different arguments, and to produce different results

The () Operator Invokes the Function

Example: ‘function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } document.getElementById(“demo”).innerHTML = toCelsius’

Functions Used As Variable Values

Example: Instead of using a variable to store the return value of a function: ‘let x = toCelsius(77); let text = “The temperature is “ + x + “ Celsius”;’

Use the function directly as a variable value: ‘let text = “The temperature is “ + toCelsius(77) + “ Celsius”;’

Local Variables

Example: ‘ // code here can NOT use carName

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

// code here can NOT use carName’


JS Operators

Source: https://www.w3schools.com/js/js_operators.asp

Example:

  1. ‘let x = 10;’

Example: ‘let x = 5; let y = 2; let z = x + y;’

Example: ‘let x = 5; let y = 2; let z = x * y;’

Types of JS Operators

  1. Arithmetic: perform arithmetic on numbers: ‘let a = 3; let x = (100 + 50) * a;’
  2. Assignment: assign values to variables
    • Addition Assignment Operator (+=) adds value to a variable: ‘let x = 10; x += 5;’
    • The + operator AND the += operator can also be used to add (concatenate) strings.
    • When used on strings, the + operator is called the concatenation operator
    • Adding two numbers will return a sum but adding a number + string returns a string
  3. Comparison:
    • Examples:
    • == equal to === equal value and equal type != not equal !== not equal value or not equal type > greater than < less than >= greater than or equal to <= less than or equal to ? ternary operator
  4. Logical:
    • Examples:
    • && logical and     logical or ! logical not
  5. Conditional
  6. Type:
    • Examples:
    • typeof returns the type of a variable instanceof returns true if an object is an instance of an object type

Things I Want To Know More About

Nothing at this time.

URL: https://s-glass.github.io/reading-notes/102/class7notes