-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathjavascript-bible.js
More file actions
213 lines (167 loc) · 5.66 KB
/
Copy pathjavascript-bible.js
File metadata and controls
213 lines (167 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
///////////////////////// JAVASCRIPT BIBLE ////////////////////////////
// --------------------------------------------------------------------
// Variables:
// --------------------------------------------------------------------
// data types:
// String (denoted by quotation marks)
// Number
// Boolean (true or false)
// Array []
// Object {}
// declaring variables
var x = 23;
var name = "Brandy";
// variables can be assigned to other variables
var myName = name;
var age = x;
// concatination vs addition
var firstName = "Jacob";
var lastName = "Hansen";
console.log(firstName + " " + lastName); // Jacob Hansen
console.log(9 + 12); // 21
console.log("9" + 12); // 912
console.log("9" * 12); // NaN
// mathematical operators
x = x + 12; /* is the same as: */ x += 12;
x = x - 12; /* is the same as: */ x -= 12;
x = x * 12; /* is the same as: */ x *= 12;
x = x / 12; /* is the same as: */ x /= 12;
// to increment/decrement a number/variable by 1:
x++;
x--;
var x = 10;
console.log(x++); // 10
// if the `++` or `--` are before the variable then it adds/subtracts 1 BEFORE it is used
++x;
--x;
var x = 10;
console.log(++x); // 11
// --------------------------------------------------------------------
// to `escape` use backslash
console.log("This text\'s quotes will \"display\""); // This text's quotes will "display"
// --------------------------------------------------------------------
// Type conversion:
// --------------------------------------------------------------------
// string to integer (parseInt()):
var age = "29";
var inegerAge = parseInt(age);
// string to float (parseFloat()):
var balance = "67.89";
var foatBalance = parseFloat(balance);
// data to string
var stringAge = String("25");
var nan = String(NaN);
// --------------------------------------------------------------------
// Type Checking:
// --------------------------------------------------------------------
// array:
Array.isArray([-1, 0, 5]); // true
// NaN:
isNaN("Hi!"); // true
isNaN(NaN); // true
isNaN(69); // false
// --------------------------------------------------------------------
// Popup Boxes:
// --------------------------------------------------------------------
alert("This will be alerted.");
// confirm returns a boolean
var accept = confirm("Do you agree you are above 18?");
// prompt returns a string
var userName = prompt("What is your name?");
// --------------------------------------------------------------------
// DOM Manipulation:
// --------------------------------------------------------------------
// to 'get' element from DOM:
document.querySelector('*selector*');
// to set element from DOM to a JavaScript variable:
// in html:
// <div class="my-div">This is my div, there are many other like it but this one's mine!</div>
// in JavaScript:
var myDiv = document.querySelector('.my-div');
// to get/set text in `myDiv`:
myDiv.textContent = "This will overwrite previous content.";
// another way to do it with 'injecting' html:
myDiv.innerHTML = "<h1>This will overwrite previous content AND be a heading!</h1><p>Add as many tags as you like!</p>";
// you can also alter CSS
myDiv.style.backgroundColor = "purple";
myDiv.style.border = "2px solid blue";
// --------------------------------------------------------------------
// Conditional Logic Flow
// --------------------------------------------------------------------
// predicates are anything that can be resolved to either `true` or `false`
// i.e. 5 > 1, "this" === "that", true
// syntax:
if (predicate) {
// code to run if predicate resolves to `true`
}
// example:
var password = "rubberduckie";
if (password === "rubberduckie") {
console.log("Correct password.");
} else if (password === "rubberduck") {
console.log("Almost correct password.");
} else {
console.log("Incorrect password");
}
// combining multiple predicates using && (and) and || (or):
var isCool = true;
var name = "Jim";
if (isCool && name === "Jim") {
console.log("You're cool & your name is Jim.");
} else if (!isCool && name === "Jim") {
console.log("You're not cool but you're name is Jim.");
} else if (isCool || name === "Jim") {
console.log("You're either cool or your name is Jim.");
} else {
console.log("You're not cool and you're name's not Jim.");
}
// Ternary Operator:
// syntax:
// predicate ? 'if true, this will run' : 'if false, this will run'
// example:
var isMember = true;
var feePrompt = "The fee is " + (isMember ? "$2.00" : "$10.00"); // The fee is $2.00
// Default Operator:
// assigns a value if one is not given (think default value)
var greeting = "Hello there, " + (prompt("What's your name?") || "anonymous");
// Guard Operator:
// sets value to second condition if first condition is true
var secretCode = isAdmin && "The password is 'cheese'";
// Switch Statement:
//syntax:
// note: anything in `[ ]` is optional
/*
switch (expression/variable) {
case value1:
//Statements executed when the result of expression matches value1
[break;]
case value2:
//Statements executed when the result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the result of expression matches valueN
[break;]
default:
//Statements executed when none of the values match the value of the expression
[break;]
}
*/
// example:
var fruit = "Papayas";
switch (fruit) {
case "Bananas":
console.log("Bananas are $0.48 a pound.");
break;
case "Cherries":
console.log("Cherries are $3.00 a pound.");
break;
case "Mangoes":
case "Papayas":
console.log("Mangoes and papayas are $2.79 a pound.");
break;
default:
console.log("Sorry, we are out of " + fruit + ".");
}
// Mangoes and papayas are $2.79 a pound.
// last update: 11/17/15 by Christoph the Wolf