View on GitHub

Code Fellows reading notes

A repository for organizing notes from my learning.

Programming with JavaScript

Expressions and Operators

Reference: MDC Javascript Guide: Expressions and operators

Operators

JavaScript has binary, unary, and ternary operators.

A binary operator requires 2 operands:

//operand1 operator operand2;
3 + 4;
x * y;

A unary operator only requires 1 operand:

//operator operand;
//or
//operand operator;
x++;
++x;

Assignment Operators

An assignment operator assigns a value to its left operand based on the value of its right operand. The simplest example of this is equal =.

JS also includes multiple compound assignment operators that serve as shorthand for operations. For an in-depth table listing of these operators, refer the reference document listed on this page.

Return Values and Chaining

Assignments like x = y have a return value, which can be retrieved by assigning or logging the assignment.

x = y //returns y;
x += y //returns x + y;
x **= y // returns x ** y;

Logical assignments return the value of the logical operation they perform.

Note: Return values are always based on the operands’ value before the operation.

When chaining expressions, they evaluate right-to-left. This opposite from what we’re used to. w = z = x = y is equivalent to w = (z = (x = y))

Destructuring

The destructuring assignment makes it possible to extract data from arrays or objects. The syntax is similar to the construction of arrays, and allows for much simpler execution:

var foo = ['one', 'two', 'three'];

// without destructuring
var one   = foo[0];
var two   = foo[1];
var three = foo[2];

// with destructuring
var [one, two, three] = foo;

Operator Types

There are many different types of operators in JS. For a detailed look at the operators under each type, refer to the reference document. The types are as follows:

Expressions

An expression is any valid unit of code that resolves to a value.

Javascript expressions are broken into categories:

Functions

Reference: MDN JavaScript Guide: Functions

Functions are essentially procedures in JavaScript. They take some input and produce an output based on a defined series of steps. Functions must be defined before they can be used.

Functions are defined using the function keyword:

function square(somevar) {
    return somevar * somevar;
}

The function square above takes one input, assigns it to somevar, and returns the result of the expression somevar * somevar.

Defining a function does not execute it. A function must be called after being defined in order to execute. If we wanted to execute our above function on the value 2 we would use square(2) and it would return 4!

A function can also call itself, as seen in this example function for calculating a factorial:

function factorial(n) {
  if ((n === 0) || (n === 1))
    return 1;
  else
    return (n * factorial(n - 1));
}

Function Scope

Variables defined inside a function cannot be accessed outside of the function. A function can, however, access all variables and functions defined inside of the scope in which it is defined. Functions defined within functions can access all variables defined in the parent, as well as all variables that the parent has access to. This organizatoin of access is considered the scope of functions.

The existence of scopes allows for what are known as closures. Closures allow a sort of encapsulation of information, in case an inner function survives longer than the outer function. The following is an example from Mozilla:

var pet = function(name) {   // The outer function defines a variable called "name"
  var getName = function() {
    return name;             // The inner function has access to the "name" variable of the outer function
  }
  return getName;            // Return the inner function, thereby exposing it to outer scopes
}
myPet = pet('Vivie');

myPet();                     // Returns "Vivie"

Using the arguments object

arguments[i] is a JS object used to access the arguements of a function. The arguements of a function are stored as an array, so arguments[0] would be used to access the first input to a function. The total number of arguments can be indicated by arguments.length. This tool can be useful when you aren’t sure many arguments will be passed to a function.

Control Flow

Reference: MDN Web Docs Glossary: Control Flow

The control flow is the order in which a computer executes statements in a script.

Code is run in order from first to last line, unless a structure specifically changes the control flow, such as conditionals and loops.

Control flow is important to keep in mind when reading script. Don’t just read top to bottom. Consider structures that change the control flow as you read.