Part 2
Prerequisiteโ
- Javascript Basics
- JavaScript: Understanding the Weird Parts in 35ish minutes - Introduction
- JavaScript: Understanding the Weird Parts in 35ish minutes - Part 1/6
- Willingness to learn
Section 3 - Types and Operatorsโ
Lesson 19 - Conceptual Aside (Types and Javascript)โ
#BIGWORD Dynamic Typing - You donโt tell the engine what type of data a variable holds, it figures it out while your code is running. Variables can hold different types of values because itโs all figured out during execution.
Other languages have Static Typing
bool isNew = โhelloโ; // an error in js
Javascript is Dynamically Typed
var isNew = true // no errors
isNew = โyup!โ;
isNew = 1;
Lesson 20 - Primitive Typesโ
There are 6 primitive types in javascript. A primitive type is a type of data that represents a single value, ie not an object
- undefined: It represents lack of existence (you shouldnโt set a variable to this)
- null: It represents lack of existence (you can set a variable to this)
- boolean: true or false
- number: Floating point number (thereโs always some decimals). Unlike other programming languages, thereโs only one number type โฆ.and it can make math weird.
- string: a sequence of characters both single and double quotes can be used.
- symbol: used in ES6 (the next version of javascript)
Lesson 21- Conceptual Aside (Operators)โ
#BIGWORD Operators - A special function that is syntactically (written) differently, Generally operators take 2 params and return a result.
Example 1
var a = 1>2; //infix notation
console.log(a); //false
In the above example + sign is the operator itโs the addition operator and its actually the function.
Lesson 22- Operators Precedence and Associativityโ
#BIGWORD Operator precedence - Which function gets called first. Functions are called in order of precedence (Higher precedence wins)
#BIGWORD Associativity - What order operator functions get called in: Left to right or right to left (When functions have the same precedence)
Example 1
var a = 3 + 4 * 5;
console.log(a); //23
Example 2
var a=2 , b =3 , c=4;
a = b = c ;
console.log(a); //4
console.log(b); //4
console.log(c); //4
They all are equal because of associativity.
Example 3
var a = (3 + 4) * 5;
console.log(a); //35
Brackets have higher precedence
Lesson 24- Conceptual Aside (Coercion)โ
#BIGWORD Coercion - Converting a value from one type to another. This happens quite often in javascript because itโs dynamically typed.
Example 1
var a = 1+2;
console.log(a); //3
Example 2
var a = โHello โ+ โWorldโ;
console.log(a); //Hello World
Example 3
var a = 1 + โ2โ;
console.log(a); //12 Coerced 1 from number to string
Lesson 25- Comparison Operatorsโ
Example 1
console.log(1<2<3); //true
Less than the operator has left to right associativity so 1 < 2 return true then true will be coerced to 1 and 1 < 3 will return true.
Example
console.log(3<2<1); //true
Less than the operator has left to right associativity so 3 < 2 returns false than false will be coerced to 0 and 0 < 1 will return true
Check
Number(undefined); // NaN
Number(null); //0
Lesson 27- Existence and Booleansโ
Check
Boolean(undefined); // false
Boolean(null); //false
Boolean(โโ); //false
Boolean(0); //false
All of these things imply the lack of existence they convert to false.
Example 1
var a ;
if (a) {
console.log(โSomething is thereโ);
}
In the above example a will be converted to boolean. We can use coercion to check if the variable has some value.
Lesson 28- Default Valuesโ
Check
undefined || โhelloโ //hello
null || โhelloโ //hello
โโ || โhelloโ //hello
**Example 1 **
function greet(name) {
name = name || โtonyโ; //OR operator behaviour
console.log(โhello โ+name);
}
greet();
Lesson 29- Framework Aside (Default Values)โ
Lib_1.js
var libraryName = โLib 1โ;
Lib_2.js
window.libraryName = window.libraryName || โLib 2โ;
Githubโ
Bookmarkโ
Unlike life, keyboards do have shortcuts, press COMMAND+D to make this an easily accessible resource by bookmarking it.
Creditsโ
- Anthony Alicea, follow him on Twitter