Fundamentals Flashcards
The Six Primitive Values
numbers, strings, booleans, objects, functions, and undefined values
Arithmetic Operators
\+ Plus Addition - Minus Subtraction * Star Multiplication / Slash Division () Parens Grouping % Percent Modulus or Remainder
Special Number Values
Infinity Positive Infinity
-Infinity Negative Infinity
NaN Not a number
All three are special values considered of the number type but don’t behave like other numbers.
NaN == NaN
false
NaN is the only value in JS that is NOT equal to itself.
Logical Operators
&& (AND)
|| (OR)
! (NOT)
The Ternary Operator
( v1 ? v2 : v3 )
IF ‘first value’ THEN ‘second value’ ELSE ‘third value’
===
!==
Precision equality. JS will not attempt any type coercion when comparing using either of these two operators. Items compared with == and != are subject to type coercion.
The var statement
var name; var name = expression; var n1 = ex1, n2 = ex2;
A variable name with no assigned expression is evaluated as ‘undefined’.
Valid variable names
Are alphanumeric, must begin with a letter, and may contain underscore ‘_’ or dollar sign ‘$’ but no other punctuation or whitespace.
Coercion functions
Number()
String()
Boolean()
How do I check if a value is not a number or cannot be converted to a number?
isNaN(value) –> true
* or *
isNaN(Number(value)) –> true
Number(value) returns NaN if the value cannot be converted.
‘do … while’ loops
do {statements} while (expression);
Do statements at least once, then keep doing them until expression evaluates as false.
‘for’ loops
for (initialization; check; update) { statements; }
‘while’ loops
while (expression) { statements; }
break
Control jumps out of the enclosing loop.
continue
Control jumps out of the loop body and the loop begins again at the beginning.
switch
switch (expr) { case label_1: statements; [break]; ... case label_2: ... }
if and else statements
if (expression) { statement; } else if (expression) { statement; } else { statement; }
function statement
var func_name = function (params) { body; };
The curly braces are not optional, no matter how short the body is.
function declaration
function name (params) { body; };
A function may be declared anywhere in the scope and calls to it will still work. Function declarations are read before execution begins.
“Optional” function arguments
If args > parameters, then the additional arguments are ignored. If args < parameters, the missing parameters are assigned the value undefined.
Arrays
Store an ordered sequence of values. Array literals are created by separating the values with commas and wrapping the whole in square brackets.
Getting Properties
Two ways. Direct naming via dot notation and evaluative access via square brackets.
Methods
properties that contain function values are called methods.
Objects
Arbitrary collections of properties. Syntax: var object_name = { 'prop name': value_expression, prop2: val_exp, };
The delete operator
A unary operator that deletes a property from an object.
delete obj_name.prop_name
delete obj_name[“prop name”]
Object property assignment
obj_name[“prop_name”] = value
will assign the value to the property of the object. If the property does not exist it is created.
The in operator
“string” in object
A binary operator applied to a string and an object that will return true if the string names a property of the object.
Mutable and Immutable values
Objects are mutable. Arrays are a special case of objects, therefore they are mutable.
Numbers, Strings, and Booleans are Immutable.
Object Comparison
name1 == name2 will return true only if both object names refer to the same object, false otherwise - even if name1 and name2 refer to separate objects that have identical contents. There is no ‘deep’ comparison operator built-in.
Looping over the properties of an object
for (var elem in object) {
do whatever to elem;
do whatever to object[‘elem’];
}
This is very pythonic - essentially a python for statement. It loops over the elements of the object.
The arguments object
When a function is called, a special variable named arguments is created for that local environment - it stores the arguments that were passed to the function. The arguments object is a lot like an array but it supports no array methods. It does however have a length property and it’s elements can be accessed via index (i.e. arguments[x] gets the value at x).
The Math object
A container for a bunch of mathematical stuff. Properties: max min floor ceil round PI random * trig stuff --> sin, cos, tan, etc.
The Global object
The object where each global variable is stored. In browsers, the global scope object is stored in the window variable.
JSON
JavaScript Object Notation
Very similar to objects and arrays with some restrictions:
1. All property names are wrapped in double quotes
2. No comments allowed.
3. Only simple data types - no function calls, etc.
JSON.stringify(value)
takes a JS value and returns a JSON encoded string
JSON.parse(string)
Takes a JSON encoded string and returns the value it encodes.