Graduate Program KB

Foundations of Javascript - Introduction

Values

  • Primitive Values is data that is not an object and has no methods and properties. They are also immutable, you can replace the value but value can't be directly altered.
    • Numbers
    • Strings
    • Boolean
    • null
    • undefined
    • bigInt
    • symbol
        let varStr = "this is a string"
        varStr[0] = "T" //Replace 1st character
        console.log(varStr) //this is a string
        //Notice how the "T" doesn't replace the first character
        varStr = "Changed string"
        console.log(varStr) //Changed string
        //You can reassign new values, but can't modify the current value.
    
  • Non-Primitive Values are objects. They are mutable, so you can directly modify the value without creating a new value.
    • array
    • functions

Operations

  • Operands are objects that are being operated on
    • 3 + 4
      • 3 and 4 are operands
      • + is the operator
  • Binary Operations have 2 operands involved
    • 3 + 4
  • Unary Operations have 1 operand
    • ii++
      • ii is operand
      • ++ is operator

Types

  • Values can have a specific data Types
  •     typeof <value>
        //Tells you what type of data type the value is 
    

Variables

  • A named reference to a value, you can access a value by calling its' name. These variables are stored in memory when they are created.
  •   var varName = "String"
      var varNum = 100
    

Expressions vs Statements

  • Expressions resolve to a value
  • Statement is made up of multiple expressions and is the full sentence.
  •   var varName = 100;
      //100 is an expression
      //varName is an expression
      //var varName is expression
      //varName = 100 expression
      //var varName = 100l; //is a statement
    

Condition Checking

  • If Statement
    if(condition){
        //Do this if conditon is true
    } else {
        //If condition is false, then do stuff here
    }

Loops

  • Repeats a operation depending on the context
  • For Loops
    const arr = [1,2,3,4,5]

    for (let index = 0; index < arr.length; index++) {
        console.log(arr[index])
    }
    //Prints out array from elements 0 to 4
  • For Each
    const arr = [1,2,3,4,5]
    //Goes through each element in array
    arr.forEach(element => {
        console.log(element)    
    });
  • While Loops
    while(condition) {
        //do something until the condition is false.
        //Would need to do something here to change the condition to false otherwise it will loop forever
    }

Functions

  • Collection of statements, that can be run by calling the function name
    function calcTotal(inputParams) {
        //Statement inside here
        //Do stuff with inputParams
    }
    calcTotal(1235); //Instead of copying the statements in the function, you can just call the function name. A shortcut
    
    //Functions can return a value so then you can assign a variable to the function call
    var total = calcTotal(123)