Skip to main content

Part 2

Prerequisiteโ€‹

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โ€‹

Follow me on Github.


Bookmarkโ€‹

Unlike life, keyboards do have shortcuts, press COMMAND+D to make this an easily accessible resource by bookmarking it.

Creditsโ€‹