You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
breaking out of loop: break jumps immediately out of enclosing loop
continue jumps out of the body and continues with the loop's next iteration
switch construct
switch(prompt("some question")){case"1":
statements;break;case"2":
statements;// also runs the next case because no breakcase"3":
statements;break;default:
statements;break;}
Updating Variables Succinctly
use += or -= or *=
incremental: use ++ or --
Comments
for single-line comments, use //
for sections, use /* blah blah blah */
Functions
wrapping a piece of program in a value
Defining a Function
function definition is a regular variable definition
varsquare=function(x){returnx*x;}
a return keyword without an expression after causes function to return undefined
Parameters and Scopes
parameters to function behave like regular variables but initial values are given by caller of the function
variables created inside of function are local to the Function
variables declared outside of functions are global, as they are visible throughout the program
Nested Scope
functions can be created inside of other functions
lexical scoping: variables from block around function's definition are visible, function bodies that enclose it are at top level of program
in Javascript, the only things that create a new scope are functions
let creates a variable that is local to the enclosing block not the enclosing function
Functions as Values
function variables act as names for a specific piece of the program
function values can do all the other things that other values can do (use in arbitrary expression, store function value in new place, pass as an argument to function, etc)
Declaration Notation
functionsquare(x){returnx*x;}
works even if function is defined code that uses it
however cannot put such a function definition inside a conditional block/loop (use function expression)
The Call Stack
computer stores the context of flow of control of program in call stack
every time a function is called, the current context is put on the top of the "stack"
when the function returns, it removes the top context from the stack and continues execution
Optional Arguments
if pass a function too many arguments, the extra ones are ignored
if pass a function too few, the missing parameters are assigned the value undefined
good for taking in "optional" arguments; if there aren't enough arguments passed then if it's undefined, assign the default value
Closure
closure: being able to reference a specific instance of local variables in enclosing function
a closure is a function that "closes over" some local variables
find some functionality haven't written yet and deserves its own function
Functions and Side Effects
functions can be divided into those that are called for the side effects and the ones that are called for their return values (though it's possible to have both)
pure function: value-producing function that not only has no side effects but also doesn't rely on side effects from other code
always produces the same value with the same arguments
Data Structures: Objects and Arrays
objects allow for grouping values together and building more complex structures
Data Sets
array: list of values between square brackets, separated by commas
look up elements by the indices
Properties
there are expressions that access a property of some value
almost all JS values have properties with the exceptions of null and undefined
access properties: using a dot and using square brackets
dot: part after dot must be valid variable name, directly names the property
square brackets: expression between brackets is evaluated to get property name
Methods
examples: thing.push(), thing.toUpperCase()
Objects
object: arbitrary collection of properties; can add or remove properties as desired
to create an object, we can use curly brace notation
within the curly braces, a list of properties is given separated by commas
binary in operator: returns Boolean value that indicates whether object has particular property
Mutability
immutable: impossible to change value (types: numbers, strings, Booleans)
Javascript's == operator only returns true if two objects are precisely the same value (meaning that they are the same object and have identical contents)