-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsHandbook.js
More file actions
156 lines (128 loc) · 3.03 KB
/
jsHandbook.js
File metadata and controls
156 lines (128 loc) · 3.03 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
//Variables and Data Types
var myVar = "Hello World"; //String
var myNum = 123; //Number
var myBool = true; //Boolean
var myNull = null; //Null
var myUndefined = undefined; //Undefined
let myLet = "Hello"; // Block Scope
const myConst = [1, 2, 3]; // Block Scope
//Arithmetic Operators
var sum = 1 + 2;
var difference = 3 - 1;
var product = 2 * 3;
var quotient = 10 / 2;
var remainder = 10 % 3;
//Comparison Operators
var equal = 1 == "1"; //true
var strictEqual = 1 === "1"; //false
var notEqual = 1 != "1"; //false
var strictNotEqual = 1 !== "1"; //true
var greaterThan = 2 > 1; //true
var lessThan = 1 < 2; //true
var greaterThanOrEqual = 2 >= 2; //true
var lessThanOrEqual = 1 <= 1; //true
//Logical Operators
var and = true && true; //true
var or = false || true; //true
var not = !true; //false
//Conditionals
if (condition) {
// Code to run if condition is true
} else if (condition2) {
// Code to run if condition2 is true
} else {
// Code to run if both conditions are false
}
//For Loop
for (var i = 0; i < 10; i++) {
// Code to run
}
//While Loop
var i = 0;
while (i < 10) {
// Code to run
i++;
}
//Functions
function myFunction(param1, param2) {
// Code to run
return result;
}
//Anonymous Function
var myFunction = function (param1, param2) {
// Code to run
return result;
};
//Arrow Function
const myFunction = (param1, param2) => {
// Code to run
return result;
};
// Arrow Function with Implicit Return
const myFunction = (param1, param2) => result;
//Arrays
var myArray = [1, 2, 3, 4, 5];
var myArray2 = new Array(1, 2, 3, 4, 5);
//Accessing Array Elements
var firstElement = myArray[0];
//Adding Elements to an Array
myArray.push(6);
//Removing Elements from an Array
myArray.pop();
// Array Destructuring
const [first, second, ...rest] = myArray;
// Spread Operator
const newArray = [...myArray1, ...myArray2];
//Objects
var myObject = {
key1: "value1",
key2: "value2",
key3: "value3",
};
//Accessing Object Properties
var value1 = myObject.key1;
//Adding Properties to an Object
myObject.key4 = "value4";
//Removing Properties from an Object
delete myObject.key4;
// Object Destructuring
const { firstName, lastName, age } = myObject;
// Rest Operator
const myFunction = (param1, param2, ...restParams) => {
// Code to run
};
// Class Definition
class MyClass {
constructor(param1, param2) {
this.param1 = param1;
this.param2 = param2;
}
myMethod() {
// Code to run
}
}
//Promises allow for asynchronous programming and provide a more readable way of handling callbacks.
const myPromise = new Promise((resolve, reject) => {
// Code to run
if (success) {
resolve(result);
} else {
reject(error);
}
});
// Promise Chain
myPromise
.then((result) => {
// Code to run on successful completion
})
.catch((error) => {
// Code to run on error
});
//ES6 introduced module syntax for organizing code into separate files.
// Export Syntax
export const myVar = 5;
export function myFunction() {
// Code to run
}
// Import Syntax
import { myVar, myFunction } from "./myModule.js";