-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjavascript_reference.js
More file actions
1254 lines (1025 loc) · 39.1 KB
/
javascript_reference.js
File metadata and controls
1254 lines (1025 loc) · 39.1 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------------------
// JavaScript Reference and Guide
//
// ReferenceCollection.com
// Licensed under CC BY-SA
// ---------------------------------------------------------------------------------
// TABLE OF CONTENTS
// -----------------
// 1. Introduction
// 2. Basic Syntax and Structure
// 3. Modules
// 4. Variables and Data Types
// 5. Operators
// 6. Control Flow Statements
// 7. Arrays
// 8. Functions
// 9. Objects
// 10. DOM Manipulation
// 11. Events
// 12. Asynchronous JavaScript
// 13. Error Handling
// 14. Classes
// 15. Iterators and Generators
// 16. Debugging
// ---------------------------------------------------------------------------------
// 1. Introduction
// ---------------------------------------------------------------------------------
// JavaScript is a versatile, high-level, dynamic programming language primarily
// known for adding interactivity to websites. It's a core technology of the web,
// alongside HTML and CSS. It is also used in server-side environments (Node.js),
// mobile app development (React Native), and even desktop applications (Electron).
// Key Features:
// - Dynamic Typing: Variables can hold different types of data
// - First-class Functions: Functions can be assigned to variables
// - Event-Driven: Responds to user actions and system events
// - Rich Ecosystem: Vast collection of libraries and frameworks
// - Cross-Platform: Runs in browsers, servers, and various environments
// - Multi-Paradigm: Supports procedural, object-oriented, and functional programming
// Getting Started:
// - No installation is required for browser-based JavaScript.
// - For server-side development, install Node.js (which includes npm, the Node
// Package Manager).
// - Use a text editor or IDE (Integrated Development Environment) like VS Code.
// - JavaScript code can be embedded in HTML files using `<script>` tags or linked
// as separate `.js` files.
// JavaScript Engine:
// A program that executes JavaScript code. Popular engines include:
// - V8 (Chrome, Node.js, Edge)
// - SpiderMonkey (Firefox)
// - JavaScriptCore (Safari)
// ECMAScript:
// The standard upon which JavaScript is based. It defines the core features.
// New versions (ES6, ES2017, ES2020, etc.) introduce new features and improvements.
// It's often referred to as "ES" followed by the year of release.
// ---------------------------------------------------------------------------------
// 2. Basic Syntax and Structure
// ---------------------------------------------------------------------------------
// JavaScript programs are made up of statements, expressions, and declarations.
// Basic Elements:
// - Statements: Instructions that perform actions.
// - Expressions: Produce values.
// - Variables: Named storage for data.
// - Functions: Reusable blocks of code.
// - Objects: Collections of related data and functions.
// Syntax Rules:
// - Case-Sensitive: `myVariable` and `myvariable` are distinct.
// - Semicolons: Generally optional, but highly recommended to avoid ambiguity.
// - Comments: `//` for single-line, `/* ... */` for multi-line.
// - Whitespace: Mostly ignored, but crucial for readability.
// - Strict Mode: ` "use strict";` at the beginning of a file or function enables
// stricter error handling and prevents some unsafe actions.
// Example:
"use strict"; // Enable strict mode
function greet(name) { // Function declaration
return `Hello, ${name}!`; // String interpolation
}
console.log(greet("World")); // Function call and output
// ---------------------------------------------------------------------------------
// 3. Modules
// ---------------------------------------------------------------------------------
// Modules help organize code into reusable units, preventing naming conflicts and
// improving maintainability.
// Key Concepts:
// - `export`: Makes variables, functions, or classes available to other modules.
// - `import`: Brings functionality from other modules into the current module.
// Example (module1.js):
// Exporting a constant
export const myConstant = 42;
// Exporting a function
export function myFunction(x) {
return x * 2;
}
// Exporting a class
export class MyClass {
constructor(value) {
this.value = value;
}
}
// Default export (only one per module)
export default function myDefaultFunction() {
console.log("This is the default export.");
}
// Example (module2.js):
// Importing specific exports
import { myConstant, myFunction } from './module1.js';
// Importing the default export
import myDefaultFunction from './module1.js';
// Importing everything with a namespace
import * as MyModule from './module1.js';
console.log(myConstant); // Accessing the imported constant: 42
console.log(myFunction(5)); // Calling the imported function: 10
myDefaultFunction(); // Calling the default export function: "This is the default export."
console.log(MyModule.myConstant) // Accessing using the namespace: 42
// ---------------------------------------------------------------------------------
// 4. Variables and Data Types
// ---------------------------------------------------------------------------------
// Variables are used to store data. JavaScript is dynamically typed, meaning
// you don't need to specify the type of a variable when you declare it.
// Variable Declarations:
// - let: Block-scoped, can be reassigned.
// - const: Block-scoped, must be initialized, cannot be reassigned.
// - var: Function-scoped (global if declared outside any function).
// Avoid using var in modern JavaScript.
// Primitive Data Types:
// - `number`: Numeric values (integers and floating-point numbers).
// - `string`: Textual data.
// - `boolean`: `true` or `false`.
// - `undefined`: Represents a variable that is declared but not assigned a value.
// - `null`: Represents the intentional absence of any object value.
// - `symbol`: Unique and immutable values, often used as object property keys.
// - `bigint`: Represents integers larger than the `number` type can handle.
// Reference Data Types:
// - `object`: Collections of key-value pairs.
// - `array`: Ordered lists of values.
// - `function`: Reusable blocks of code.
// Examples:
let userAge = 30; // Number
const name = "Alice"; // String
let isLoggedIn = true; // Boolean
let city; // Undefined (no value assigned)
let address = null; // Null (intentional absence of value)
const id = Symbol("uniqueId"); // Symbol
let veryLargeNumber = 12345678901234567890n; // BigInt
let user = { // Object
firstName: "Bob",
lastName: "Smith",
age: 25
};
let colors = ["red", "green", "blue"];// Array
function add(a, b) { // Function
return a + b;
}
// Dynamic Typing Example:
let myVar = 10; // myVar is a number
myVar = "Hello"; // myVar is now a string
myVar = true; // myVar is now a boolean
// Type Coercion (Implicit Type Conversion):
let result = 5 + "5"; // "55" (number is coerced to a string)
let result2 = "5" * 2; // 10 (string is coerced to a number)
// Explicit Type Conversion:
let numStr = "123";
let numInt = parseInt(numStr); // Converts string to integer: 123
let floatNum = parseFloat("3.14"); // Converts string to floating-point number: 3.14
let textStr = String(42); // Converts number to string: "42"
let bool = Boolean(0); // Converts to boolean (0 is falsy, so bool is false)
// ---------------------------------------------------------------------------------
// 5. Operators
// ---------------------------------------------------------------------------------
// Operators perform actions on values (operands).
// Arithmetic Operators:
// - `+` (Addition)
// - `-` (Subtraction)
// - `*` (Multiplication)
// - `/` (Division)
// - `%` (Modulo - remainder of division)
// - `**` (Exponentiation)
// Assignment Operators:
// - `=` (Assignment)
// - `+=` (Add and assign)
// - `-=` (Subtract and assign)
// - `*=` (Multiply and assign)
// - `/=` (Divide and assign)
// - `%=` (Modulo and assign)
// - `**=` (Exponentiate and assign)
// Comparison Operators:
// - `==` (Loose equality - checks only value, performs type coercion)
// - `===` (Strict equality - checks both value and type)
// - `!=` (Loose inequality)
// - `!==` (Strict inequality)
// - `>` (Greater than)
// - `<` (Less than)
// - `>=` (Greater than or equal to)
// - `<=` (Less than or equal to)
// Logical Operators:
// - `&&` (Logical AND)
// - `||` (Logical OR)
// - `!` (Logical NOT)
// Unary Operators:
// - `-` (Negation)
// - `+` (Unary plus - attempts to convert to a number)
// - `++` (Increment)
// - `--` (Decrement)
// - `typeof` (Returns a string indicating the type of a variable)
// - `delete` (Deletes an object property)
// - `void` (Discards a return value)
// Ternary Operator:
// - `condition ? exprIfTrue : exprIfFalse` (Conditional operator)
// Bitwise Operators: (less commonly used)
// - `&` (Bitwise AND)
// - `|` (Bitwise OR)
// - `^` (Bitwise XOR)
// - `~` (Bitwise NOT)
// - `<<` (Left shift)
// - `>>` (Right shift - sign-propagating)
// - `>>>` (Right shift - zero-fill)
// Examples:
let x = 10;
let y = 5;
// Arithmetic
let sum = x + y; // 15
let diff = x - y; // 5
let product = x * y; // 50
let quotient = x / y; // 2
let remainder = x % y; // 0
let power = x ** y; // 100000
// Assignment
x += 5; // x is now 15
y -= 2; // y is now 3
// Comparison
console.log(x == y); // false
console.log(x === "15"); // false (strict equality checks type)
console.log(x != y); // true
console.log(x > y); // true
// Logical
let a = true;
let b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false
// Unary
let z = -x; // -15
let strNum = +"42"; // 42 (converted to number)
let preIncrement = ++x; // x is now 16, preIncrement is 16
let postIncrement = y++; // postIncrement is 3, y is now 4
console.log(typeof z); // "number"
// Ternary
let age = 20;
let status = age >= 18 ? "adult" : "minor"; // "adult"
// ---------------------------------------------------------------------------------
// 6. Control Flow Statements
// ---------------------------------------------------------------------------------
// Control flow statements determine the order in which code is executed.
// Conditional Statements:
// - `if...else`: Executes code based on a condition.
// - `switch`: Executes code based on the value of an expression.
// if...else
let num = 10;
if (num > 0) {
console.log("Positive");
} else if (num < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
// switch
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week");
break;
case "Friday":
console.log("Almost the weekend");
break;
default:
console.log("Another day");
}
// Looping Statements:
// - `for`: Repeats a block of code a specific number of times.
// - `while`: Repeats a block of code as long as a condition is true.
// - `do...while`: Similar to `while`, but the code block is executed at least once.
// - `for...in`: Loops over properties of an object (not recommended for arrays).
// - `for...of`: Loops over the values of an object (arrays, strings, maps, sets, etc.).
// for
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}
// while
let i = 0;
while (i < 5) {
console.log(i); // 0, 1, 2, 3, 4
i++;
}
// do...while
let j = 0;
do {
console.log(j); // 0, 1, 2, 3, 4
j++;
} while (j < 5);
// for...in (iterates over object keys)
const obj = { a: 1, b: 2, c: 3 };
for (const key in obj) {
console.log(key); // "a", "b", "c"
console.log(obj[key]); // Accessing value using the key: 1, 2, 3
}
// for...of (iterates over iterable values)
const arr = [10, 20, 30];
for (const value of arr) {
console.log(value); // 10, 20, 30
}
const str = "hello";
for (const char of str) {
console.log(char); // "h", "e", "l", "l", "o"
}
// Transfer Statements:
// - `break`: Exits a loop or switch statement.
// - `continue`: Skips the current iteration of a loop and continues with the next.
// - `return`: Exits a function and optionally returns a value.
// break
for (let k = 0; k < 10; k++) {
if (k === 5) {
break; // Exit the loop when k is 5
}
console.log(k); // 0, 1, 2, 3, 4
}
// continue
for (let m = 0; m < 5; m++) {
if (m === 2) {
continue; // Skip the iteration when m is 2
}
console.log(m); // 0, 1, 3, 4
}
// return
function add(a, b) {
return a + b; // Exit the function and return the sum
}
let sumResult = add(3, 4); // 7
// ---------------------------------------------------------------------------------
// 7. Arrays
// ---------------------------------------------------------------------------------
// Arrays are ordered collections of values. They can hold elements of any data
// type, including other arrays.
// Declaration and Initialization:
let emptyArray = [];
let numbers = [1, 2, 3, 4, 5];
let mixedArray = [1, "hello", true, { name: "Alice" }];
// Accessing Elements:
// Use square brackets and the index (starting from 0).
console.log(numbers[0]); // 1
console.log(mixedArray[1]); // "hello"
// Modifying Elements:
numbers[2] = 10; // Change the element at index 2
console.log(numbers); // [1, 2, 10, 4, 5]
// Array Length:
console.log(numbers.length); // 5
// Adding Elements:
numbers.push(6); // Add to the end: [1, 2, 10, 4, 5, 6]
numbers.unshift(0); // Add to the beginning: [0, 1, 2, 10, 4, 5, 6]
// Removing Elements:
let lastElement = numbers.pop(); // Remove from the end (returns 6)
let firstElement = numbers.shift(); // Remove from the beginning (returns 0)
console.log(numbers); // [1, 2, 10, 4, 5]
// Other Common Array Methods:
// - `concat()`: Joins arrays.
// - `join()`: Converts elements to a string.
// - `slice()`: Extracts a section into a new array.
// - `splice()`: Adds/removes elements (modifies original).
// - `indexOf()`: Returns first index of an element.
// - `includes()`: Checks if array includes a value.
// - `find()`: Returns first element satisfying a condition.
// - `findIndex()`: Returns index of first element satisfying a condition.
// - `filter()`: Creates new array with elements passing a condition.
// - `map()`: Creates new array by applying function to elements.
// - `forEach()`: Executes function for each element (no return).
// - `reduce()`: Reduces array to a single value.
// - `sort()`: Sorts elements (modifies original).
// - `some()`: Checks if any element passes a test.
// - `every()`: Checks if all elements pass a test.
// - `flat()`: Flattens nested arrays to a depth.
// Examples:
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let combined = arr1.concat(arr2); // [1, 2, 3, 4, 5, 6]
let joined = arr1.join("-"); // "1-2-3"
let sliced = arr1.slice(1, 3); // [2, 3] (from index 1 up to, but not including, 3)
// Removes 1 element from index 1, inserts 7 and 8. Returns removed elements: [2].
let spliced = arr1.splice(1, 1, 7, 8); // arr1 is now [1, 7, 8, 3]
console.log(arr1.indexOf(3)); // 3
console.log(arr1.includes(7)); // true
let found = arr1.find(element => element > 2); // 7 (first element greater than 2)
let filtered = arr1.filter(element => element > 2); // [7, 8, 3]
let mapped = arr1.map(element => element * 2); // [2, 14, 16, 6]
arr1.forEach(element => console.log(element)); // Prints each element
// 19 (sum of elements)
let sumArr = arr1.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
let unsorted = [3, 1, 4, 1, 5, 9, 2, 6];
// Sorts alphabetically (for strings) or numerically (with a compare function)
unsorted.sort();
let nested = [1, [2, [3, [4]]]];
let flattened = nested.flat(Infinity); // [1, 2, 3, 4]
// ---------------------------------------------------------------------------------
// 8. Functions
// ---------------------------------------------------------------------------------
// Functions are reusable blocks of code that perform a specific task.
// Function Declaration:
function greet(name) {
console.log(`Hello, ${name}!`);
}
// Function Expression:
const add = function(a, b) {
return a + b;
};
// Arrow Function (ES6):
const multiply = (a, b) => a * b;
// Function Call (Invocation):
greet("World"); // "Hello, World!"
let sumFunc = add(5, 3); // 8
let productFunc = multiply(4, 6); // 24
// Parameters and Arguments:
// - Parameters are the variables listed in the function definition.
// - Arguments are the actual values passed to the function when it's called.
// Default Parameters (ES6):
function greetWithDefault(name = "Guest") {
console.log(`Hello, ${name}!`);
}
greetWithDefault(); // "Hello, Guest!"
greetWithDefault("Alice"); // "Hello, Alice!"
// Rest Parameters (ES6):
// Allows a function to accept an indefinite number of arguments as an array.
function sum(...numbers) {
return numbers.reduce((sum, num) => sum + num, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
// Return Value:
// Functions can return a value using the `return` statement. If no `return`
// statement is present, the function returns `undefined`.
// Scope:
// - Variables declared inside a function are local to that function.
// - `let` and `const` are block-scoped (within `{}` blocks).
// Closures:
// A closure is a function that has access to variables from its outer (enclosing)
// function's scope, even after the outer function has finished executing.
function outerFunction() {
let outerVar = "Hello";
function innerFunction() {
console.log(outerVar); // innerFunction has access to outerVar (closure)
}
return innerFunction;
}
let myClosure = outerFunction();
myClosure(); // "Hello"
// Immediately Invoked Function Expression (IIFE):
// A function that is executed immediately after it's defined. Used to create
// private scope.
(function() {
let privateVar = "This is private";
console.log(privateVar);
})();
// console.log(privateVar); // Error: privateVar is not defined (outside the IIFE)
// Higher-Order Functions:
// Functions that take other functions as arguments or return functions.
function applyOperation(a, b, operation) {
return operation(a, b);
}
let resultHOF = applyOperation(5, 3, add); // 8
let result2HOF = applyOperation(5, 3, multiply); // 15
// Callback Functions:
// Functions passed as arguments to other functions, to be executed later. Common
// in asynchronous operations.
function fetchData(callback) {
setTimeout(() => {
const data = { name: "Example Data" };
callback(data); // Call the callback function with the fetched data
}, 1000);
}
fetchData(data => {
console.log("Received data:", data); // Received data: {name: 'Example Data'}
});
// ---------------------------------------------------------------------------------
// 9. Objects
// ---------------------------------------------------------------------------------
// Objects are collections of key-value pairs. Keys are strings (or Symbols),
// and values can be any data type, including other objects or functions.
// Object Literal Notation:
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
address: {
street: "123 Main St",
city: "Anytown"
},
greet: function() { // Method
console.log(`Hello, my name is ${this.firstName}`);
}
};
// Accessing Properties:
// - Dot notation: `object.property`
// - Bracket notation: `object["property"]` (useful when the property name is dynamic)
console.log(person.firstName); // "John"
console.log(person["lastName"]); // "Doe"
console.log(person.address.city); // "Anytown"
// Modifying Properties:
person.age = 31;
person["lastName"] = "Smith";
// Adding Properties:
person.email = "john.doe@example.com";
// Deleting Properties:
delete person.age;
// Methods:
// Functions that are properties of an object.
person.greet(); // "Hello, my name is John"
// `this` Keyword:
// Inside a method, `this` refers to the object the method belongs to.
// Constructor Functions:
// Used to create multiple objects of the same type.
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.greet = function() {
console.log(`Hello, my name is ${this.firstName}`);
};
}
const person1 = new Person("Alice", "Smith", 25);
const person2 = new Person("Bob", "Jones", 30);
person1.greet(); // "Hello, my name is Alice"
// Prototypes:
// Every JavaScript object has a prototype, which is another object. Objects
// inherit properties and methods from their prototype.
// The `prototype` property of a constructor function is used to add properties
// and methods to all instances created by that constructor.
Person.prototype.getFullName = function() {
return `${this.firstName} ${this.lastName}`;
};
console.log(person1.getFullName()); // "Alice Smith"
// Object.create():
// Creates a new object with the specified prototype object and properties.
const animal = {
makeSound: function() { console.log("Generic animal sound"); }
};
const dog = Object.create(animal);
dog.bark = function() { console.log("Woof!"); };
dog.makeSound(); // "Generic animal sound"
dog.bark(); // "Woof!"
// Object Destructuring:
// A concise way to extract properties from objects into variables.
const { firstName, lastName } = person;
console.log(firstName); // "John"
console.log(lastName); // "Smith"
// Spread Syntax (...):
// Used to copy properties from one object to another, or to merge objects.
const personCopy = { ...person }; // Shallow copy
const mergedObject = { ...person, occupation: "Engineer" };
// Object.keys(), Object.values(), Object.entries():
console.log(Object.keys(person));
console.log(Object.values(person));
console.log(Object.entries(person));
// ---------------------------------------------------------------------------------
// 10. DOM Manipulation
// ---------------------------------------------------------------------------------
// The Document Object Model (DOM) is a programming interface for HTML and XML
// documents. It represents the page as a tree of nodes, allowing JavaScript to
// access and manipulate the structure, style, and content of the page.
// Accessing Elements:
// - `document.getElementById(id)`: Gets element by ID.
// - `document.getElementsByClassName(className)`: Gets elements by class.
// - `document.getElementsByTagName(tagName)`: Gets elements by tag.
// - `document.querySelector(selector)`: Gets first element matching CSS selector.
// - `document.querySelectorAll(selector)`: Gets all elements matching CSS selector.
// Modifying Elements:
// - `element.textContent`: Gets/sets text content.
// - `element.innerHTML`: Gets/sets HTML content.
// - `element.setAttribute(name, value)`: Sets attribute value.
// - `element.getAttribute(name)`: Gets attribute value.
// - `element.removeAttribute(name)`: Removes attribute.
// - `element.style`: Modifies inline styles.
// - `element.classList`: Manages CSS classes (add/remove/toggle).
// Creating and Appending Elements:
// - `document.createElement(tagName)`: Creates new element.
// - `element.appendChild(childElement)`: Appends child to parent.
// - `element.insertBefore(newElement, referenceElement)`: Inserts new element before reference.
// - `element.removeChild(childElement)`: Removes child element.
// - `element.replaceChild(newChild, oldChild)`: Replaces child element.
// Examples:
// HTML:
// <div id="myDiv" class="container">
// <p>This is a paragraph.</p>
// <ul>
// <li>Item 1</li>
// <li>Item 2</li>
// </ul>
// </div>
// JavaScript:
const myDiv = document.getElementById("myDiv");
const paragraphs = document.getElementsByTagName("p");
const firstParagraph = document.querySelector("p");
const listItems = document.querySelectorAll("li");
// Accessing
console.log(myDiv.textContent); // " This is a paragraph. Item 1 Item 2 "
console.log(paragraphs[0].textContent); // "This is a paragraph."
// Modifying
firstParagraph.textContent = "This is an updated paragraph.";
myDiv.style.backgroundColor = "lightblue";
myDiv.classList.add("highlight");
// Creating and Appending
const newListItem = document.createElement("li");
newListItem.textContent = "Item 3";
const ul = document.querySelector("ul");
ul.appendChild(newListItem);
// Removing
const itemToRemove = listItems[0];
ul.removeChild(itemToRemove);
// ---------------------------------------------------------------------------------
// 11. Events
// ---------------------------------------------------------------------------------
// Events are actions or occurrences that happen in the browser (user interactions,
// page loading, etc.). JavaScript can listen for and respond to these events.
// Event Handling:
// - Inline event handlers (in HTML): `<button onclick="myFunction()">` (not recommended)
// - DOM property event handlers: `element.onclick = function() { ... };`
// - Event listeners: `element.addEventListener(eventType, function, useCapture);` (preferred)
// Common Event Types:
// - Mouse Events: `click`, `dblclick`, `mouseover`, `mouseout`, `mousedown`, `mouseup`, `mousemove`
// - Keyboard Events: `keydown`, `keyup`, `keypress`
// - Form Events: `submit`, `change`, `input`, `focus`, `blur`
// - Window Events: `load`, `resize`, `scroll`, `unload`
// - Touch Events: `touchstart`, `touchmove`, `touchend`, `touchcancel`
// Event Object:
// When an event occurs, an event object is automatically passed to the event
// handler function. This object contains information about the event (type,
// target element, mouse coordinates, key pressed, etc.).
// Event Propagation:
// - Bubbling: The event is first handled by the innermost element and then
// propagates up to the outermost element.
// - Capturing: The event is first handled by the outermost element and then
// propagates down to the innermost element. (less commonly used)
// - `event.stopPropagation()`: Prevents the event from bubbling up or capturing down.
// Preventing Default Behavior:
// - `event.preventDefault()`: Prevents the default action associated with an
// event (e.g., preventing a form submission).
// Examples:
// HTML:
// <button id="myButton">Click Me</button>
// <form id="myForm">
// <input type="text" id="myInput">
// <button type="submit">Submit</button>
// </form>
// JavaScript:
const button = document.getElementById("myButton");
const input = document.getElementById("myInput");
const form = document.getElementById("myForm");
// Event Listener
button.addEventListener("click", function(event) {
console.log("Button clicked!");
console.log("Event type:", event.type); // "click"
console.log("Target element:", event.target); // <button id="myButton">
});
// Keyboard Event
input.addEventListener("keydown", function(event) {
console.log("Key pressed:", event.key);
});
// Form Event (preventing default submission)
form.addEventListener("submit", function(event) {
event.preventDefault(); // Prevent the form from submitting
console.log("Form submitted (but prevented)");
});
// Event Propagation (Bubbling) Example:
// <div id="outer">
// <button id="inner">Inner Button</button>
// </div>
const outer = document.getElementById("outer");
const inner = document.getElementById("inner");
outer.addEventListener("click", function(event) {
console.log("Outer div clicked");
});
inner.addEventListener("click", function(event) {
console.log("Inner button clicked");
// event.stopPropagation(); // Uncomment to stop the bubbling
});
// Clicking "Inner Button
// (Output without stopPropagation: "Inner button clicked", "Outer div clicked")
// (Output with stopPropagation: "Inner button clicked")
// ---------------------------------------------------------------------------------
// 12. Asynchronous JavaScript
// ---------------------------------------------------------------------------------
// Asynchronous JavaScript allows you to execute code without blocking the main
// thread, preventing the user interface from freezing.
// Key Concepts:
// - Callbacks: Functions passed as arguments to other functions, to be executed
// later (often after an asynchronous operation completes).
// - Promises: Objects representing the eventual completion (or failure) of an
// asynchronous operation. They have states (pending, fulfilled, rejected) and
// methods like `.then()` and `.catch()` to handle the results.
// - Async/Await: Syntactic sugar built on top of Promises, making asynchronous
// code look and behave more like synchronous code.
// Common Asynchronous Operations:
// - `setTimeout()`: Executes a function after a specified delay.
// - `setInterval()`: Repeatedly executes a function at a specified interval.
// - Fetch API: Used for making network requests (e.g., fetching data from a server).
// - AJAX (Asynchronous JavaScript and XML): A set of techniques for making
// asynchronous requests to a server (Fetch API is generally preferred now).
// - Event listeners (many events are asynchronous).
// Examples:
// setTimeout
console.log("Before setTimeout");
setTimeout(() => {
console.log("Inside setTimeout (after 2 seconds)");
}, 2000); // 2000 milliseconds = 2 seconds
console.log("After setTimeout");
// Output:
// Before setTimeout
// After setTimeout
// Inside setTimeout (after 2 seconds)
// setInterval
let counter = 0;
const intervalId = setInterval(() => {
console.log("Interval:", counter);
counter++;
if (counter > 3) {
clearInterval(intervalId); // Stop the interval
}
}, 1000);
// Callback Example:
function getData(callback) {
setTimeout(() => {
const data = { message: "This is the data" };
callback(data); // Call the callback with the data
}, 1000);
}
getData(data => {
console.log("Received data:", data); // Received data: {message: 'This is the data'}
});
// Promise Example:
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const success = true;
if (success) {
resolve({ data: "Fetched data!" }); // Resolve with the data
} else {
reject("Error fetching data"); // Reject with an error message
}
}, 1500);
});
}
fetchData()
.then(result => { // .then() handles successful resolution
console.log("Success:", result.data); // Success: Fetched data!
})
.catch(error => { // .catch() handles rejection
console.error("Error:", error);
});
// Async/Await Example:
async function getDataAsync() {
try {
const result = await fetchData(); // Await the Promise resolution
console.log("Async/Await Success:", result.data); // Async/Await Success: Fetched data!
} catch (error) {
console.error("Async/Await Error:", error);
}
}
getDataAsync();
// Fetch API Example:
fetch("https://jsonplaceholder.typicode.com/todos/1") // Example API endpoint
.then(response => {
if (!response.ok) { // Check for HTTP errors
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json(); // Parse the JSON response
})
.then(data => {
console.log("Fetched data:", data); // Fetched data: {userId: 1, id: 1, title: 'delectus aut autem', completed: false}
})
.catch(error => {
console.error("Fetch error:", error);
});
// ---------------------------------------------------------------------------------
// 13. Error Handling
// ---------------------------------------------------------------------------------
// Error handling is crucial for writing robust code. JavaScript provides
// mechanisms to handle errors gracefully and prevent unexpected program crashes.
// `try...catch` Statement:
// - `try`: Encloses the code that might throw an error.
// - `catch`: Handles the error if one occurs within the `try` block.
// - `finally`: (Optional) Executes code regardless of whether an error occurred.
// `throw` Statement:
// - Used to manually throw an error (can be a built-in error object or a custom error).
// Error Object:
// - When an error occurs, a JavaScript Error object is created.
// - Properties:
// - `name`: The type of error (e.g., "Error", "TypeError", "ReferenceError").
// - `message`: A description of the error.
// - `stack`: (Non-standard, but widely supported) The call stack at the time of the error.
// Examples:
try {
// Code that might throw an error
let result = undefinedVariable + 10; // ReferenceError: undefinedVariable is not defined
} catch (error) {
console.error("Error name:", error.name); // Error name: ReferenceError
console.error("Error message:", error.message); // Error message: undefinedVariable is not defined
// console.error("Stack trace:", error.stack);
} finally {
console.log("Finally block executed"); // Finally block executed
}
// Throwing a custom error:
function divide(a, b) {
if (b === 0) {
throw new Error("Division by zero is not allowed");
}
return a / b;
}