Javascript

datatypes

0
/*
1
* DATATYPES:
2
*
3
* Datatypes are nothing more than all of the values we store/access/use
4
* throughout our javascript code.
5
*
6
* We can separate datatypes into two (2) catagories:
7
*
8
* O - Simple/Primative
9
* O - Complex
10
*
11
* Let's take a quick look at some examples of each of them!
12
*/
13
14
// Simple/Primative //
15
16
// Number //
17
var num = 1;
18
19
// String //
20
var string = 'This is a string!';
21
22
// Boolean //
23
var thisIsTrue = true;
24
var thisIsFalse = false;
25
26
// Why are they simple? //
27
28
// Atomic, immutable: they do not hold, collect or aggregate other values, //
29
// and operations on simple values return new simple values, they do not alter //
30
// the original value. //
31
32
33
// Copy by value - when assigning or passing, simple data-types are copied from //
34
// one variable to the next. //
35
36
// *!Now for the Complex!* //
37
38
// Complex //
39
40
// Object //
41
var obj = {};
42
43
// Array //
44
var arr = [];
45
46
// Function //
47
function thisIsAFunction() {};
48
49
// Why are they complex? //
50
51
// Complex values aggregate other values and therefore are of indefinite size. //
52
53
// Copy by reference - when assigning or passing, complex data-types are //
54
// passed by reference. //
55
56
var notANumber = 'NaN';
57
var nullEx = null;
58
var partyTime = undefined;
59
console.log(notANumber++);
60
console.log(partyTime); // logs undefined
61
console.log(nullEx);
62
63
// infinty and -infinity
64
console.log(Infinity ); /* Infinity */
65
console.log(Infinity + 1 ); /* Infinity */
66
console.log(Math.pow(10, 1000)); /* Infinity */
67
console.log(Math.log(0) ); /* -Infinity */
68
console.log(1 / Infinity ); /* 0 */
69
70
71
72
73

string-manipulation

0
// String //
1
2
// Character or textual data //
3
4
// Strings are character data, we MUST enclose Strings in single or double quotes //
5
var string0 = "Like this";
6
var string1 = 'Or this';
7
8
// Why are strings called strings? Like Mardi Gras beads are a string of //
9
// (cheap) pearls, strings are a string of characters, or an Array of characters. //
10
11
// Strings can be treated like an Array of characters. We can access the //
12
// individual characters of strings using what’s called, Array-syntax, or //
13
// bracket notation. //
14
15
console.log(string0[0]); // Prints 'L' to the console.
16
17
// Can change case
18
var string2 = "Hmmmmm";
19
console.log(string2.toUpperCase()); //HMMMMM
20
console.log(string2.toLowerCase()); //hmmmmm
21
22
// Can change string to an array
23
24
console.log(string2.split("")); //['h', 'm', 'm', 'm', 'm']
25
26
// Can fuse strings (cocatenate)
27
28
console.log(string0 + " " + string1); // "Like this Or this"
29
30
// Can get string length
31
32
console.log(string0.length) // logs 9
33
34
// Can replace parts of strings
35
36
string0 = string0.replace('Like','Mike');
37
console.log(string0);

control-flow

0
// CONTROL FLOW //
1
2
// JavaScript supports a compact set of statements, specifically control flow //
3
// statements, that you can use to incorporate a great deal of interactivity //
4
// in your application. //
5
6
// The semicolon (;) character is used to separate statements in JavaScript code. //
7
8
// BLOCK STATEMENTS //
9
10
// The most basic statement is a block statement that is used to group statements. //
11
// The block is delimited by a pair of curly brackets: //
12
13
// Block statements are commonly used with control flow statements //
14
// (e.g. if, for, while, switch). //
15
16
let x = 10;
17
while (x > 0) {
18
x--;
19
}
20
21
if (x === 0) {
22
console.log('BOTH STATEMENTS WORKED');
23
}
24
25
for (let i = 10; i > x; i--) {
26
console.log('The For Loop is Running');
27
}
28
29
30
// Switch Statement //
31
// Convienient When:
32
// 1. You are comparing multiple possible conditions of an expression and the
33
// 1. expression itself is non-trivial. //
34
// 2. You have multiple values that may require the same code. //
35
// 3. You have some values that will require essentially all of another value's //
36
// 3. execution, plus only a few statements. //
37
38
let z = 5;
39
switch (z) {
40
case 1:
41
console.log('No');
42
break;
43
case 2:
44
console.log('No2');
45
break;
46
case 3:
47
console.log('No3');
48
break;
49
case 4:
50
console.log('No4');
51
break;
52
default:
53
console.log("You'll get to 5 eventually");
54
break;
55
};
56
57
58
59
let exNum = 0;
60
61
// If/ElseIf/Else Example
62
if (exNum > 0) {
63
console.log('good');
64
} else if (exNum < 0) {
65
console.log('bad');
66
} else {
67
console.log('neither');
68
}
69
70

variables

0
/*
1
* VARIABLES:
2
*
3
* 0. To hold things in memory during the life-cycle of a program, we can use variables. Variables
4
* are named identifiers that can point to values of a particular type, like a Number, String,
5
* Boolean, Array, Object or another data-type. Variables are called so because once created, we
6
* can CHANGE the value (and type of value) to which they point.
7
*
8
* 1. To create a variable we use the keyword, var, followed by a name (id or alias) for our
9
* variable.
10
*
11
* 2. There are 2 phases of using variables: declaration and initialization (or assignment).
12
*/
13
14
// 1. declaration //
15
var myName;
16
17
/*
18
* At the declaration phase, the variable myName is undefined because we have NOT initialized
19
* it to anything
20
*/
21
console.log(myName); // prints => undefined
22
23
// 2. initialization or assignment //
24
myName = 'john';
25
console.log(myName); // prints => john
26
27
// 3. re-assignment //
28
myName = 'bob';
29
console.log(myName); // prints => bob
30
31
// NOTE: We can assign and re-assign anything to a variable - we cannot do this with constants //
32
var myVariable = 1;
33
myVariable = true;
34
myVariable = "someString";
35
console.log(myVariable);
36
// Constants are containers who's value can never be changed once it has been declared //
37
38
// They are created using the keyword const and must be assigned a value immediately. //
39
40
const birthDate = 02241998;
41
console.log(birthDate);
42
43
// ALL CAPS are recommended for constant names //
44
45
// Let //
46
47
// Not hoiseted to their code block //
48
49
// Limits the variable's scope to the block it's declared in. //
50
var letThemEatCake = undefined;
51
console.log(letThemEatCake);
52
53
if (birthDate === 02241998) {
54
let letThemEatCake = 'Happy Birthday!!!';
55
console.log(letThemEatCake);
56
}
57
58
console.log(letThemEatCake);
59
60
61
62
63
64
65
66
67

operators

0
// OPERATORS //
1
2
// Operatiors act on the data we have throughout our javascript. They can //
3
// assign data to variables, compare them, or include them in arithmetic //
4
// operations. //
5
6
// Operators are sorted into categories based upon what they do and the ammount //
7
// of operands-(the values that operators act on) they require. //
8
9
// Unary - 1 value //
10
// Binary - 2 //
11
// Ternary - 3 //
12
13
// COMPARISON OPERATORS //
14
15
// Comparison operators (as the name would imply) compare two values and return //
16
// either true or false. //
17
18
console.log(1 < 2);
19
console.log(5 > 3);
20
console.log(2 <= 2);
21
console.log(6 >= 5);
22
23
// STRICT COMPARISON //
24
25
// Strict comparison takes value and type of the variables into account when //
26
// comparing them. DON'T USE NON-STRICT COMPARISON UNLESS YOU ABSOLUTELY HAVE TO //
27
// (BASICALLY NEVER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!) //
28
29
console.log(2 === 2);
30
console.log(2 === '2');
31
console.log(2 !== 2);
32
console.log(2 !== '2');
33
34
// LOGICAL OPERATORS //
35
36
// && (The AND Operator): Both conditions MUST be true //
37
38
console.log(2 < 3 && 1 > 0);
39
40
// || (The OR Operator): At least one of the conditions must be true //
41
42
console.log(1 == 1 || 2 == 3);
43
44
// ! (The BANG Operator): Flips the truthiness of the value //
45
46
console.log(!true);
47
48
// !! (The (ALMIGHTY) DOUBLE BANG Operator): Coerces a value to Boolean //
49
// If the value was falsey (0, null, undefined, etc.), it will be false. //
50
// Otherwise, you'll get true instead. //
51
52
console.log(!!1);
53
54
// ARITHMETIC OPERATORS //
55
56
var a = 10;
57
console.log(1 + 1);
58
console.log(2 - 1);
59
console.log(2 * 3);
60
console.log(4 / 2);
61
console.log(4 % 2);
62
console.log(a += a); // a = 20
63
console.log(a -= 19); // a = 1
64
console.log(a *= 55); // a = 55
65
console.log(a /= 5); // a = 11
66
67
// UNARY OPERATORS //
68
// This is a set of operators that only take one operand
69
70
let i = 0;
71
i++;
72
console.log(i);
73
console.log(--i);
74
console.log(!true);
75
console.log(i--);
76
++i;
77
console.log(i);
78
79
// TERNARY OPERATORS //
80
// Shorthand if/else statements.
81
82
i === 10 ? console.log(false) : console.log(true);
83