Section 3: Types and Operators
Conceptual Aside: Types and Javascript
Dynamic Typing: with JS, you don't have to tell JS what type of data a variable holds, JS will figure that out while your code is running.
Primitive Types
Primitive type: a type of data that represents a single value. That is, not an object. There are 6 of the them:
- undefined: there's a bucket, but nothing in it.
- null: there's not even a bucket.
- boolean: true or false
- number: this includes floating numbers.
- string: a sequence of character enclosed in single or double quotes.
- symbol: new for ES6.
Conceptual Aside: Operators
Operator: Generally, operators take two parameters and return one result. Using the operators invokes a special function; operators are functions.
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus
++
Increment
--
Decrement
Operator Precedence and Associativity
Operator precedence: determines which operator function gets called first. Functions are called in order of precedence.
Associativity: if we have two or more operators of equal precedence, then the equation may move left or right. Here's handy table. For example:
var a = 10 / 2 * 100;
Is a
500 or .05? Because associativity for multiplication and division has us moving from left-to-right, a
is 500.
Conceptual Aside: Coercion
Coercion: Converting a value from one type to another (sometimes when you don't want it to). For example: var a = 1 + '2';
would return a as 12
.
To avoid coercion, use "==" equality, or "===" strict equality. Use strict by default, to keep JS from coercing anything.
Here's a comparison table using double or triple equal signs.
Comparison Operators
Comparison operators move from left-to-right. The following code would equal true
.
console.log(1 < 2 < 3);
But what about this code?
console.log(3 < 2 < 1);
It also returns true
. Huh? Let's walk through the code. Since we're going from left-to-right, JS runs this first: 3 < 2
which is false. Still moving from left-to-right, JS now sees the code as:
console.log(false < 1);
In programming, false is converted to 0 through coercion, so 0 is the less than 1!
If you try to pass undefined
as a number, you'll get NaN
.null
will return 0
.
Existence and Booleans
Coercion also occurs with booleans. You can use to check on values. That's pretty much it.
Default Values
Regarding the or ||
operation, if you pass two value to coerced to true and false, it will always return the true value. Example:
function greet(name) {
name = name || '$Your name here$';
console.log('Hello ' + name);
}
greet();
Since name was never defined in the parameter, the '$Your name here$' is returned instead. Console.log would read,Hello $Your name here$
.
Framework Aside: Default Values
What happens when libraries or plugins contain the same variables? The last variable loaded in the global execution context will take precedence. To check if a variable name is already in use, you can use a code like this:
window.libraryName = window.libraryName || "Lib 2";
This will provide a stop gap. Wouldn't it make more sense to use an && inside of ||?