You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JavaScript-Quick-Reference.md
+121-22
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,7 @@ A comprehensive yet concise quick-reference and overview of JavaScript fundament
70
70
-[9.4 Form Validation](#94-form-validation)
71
71
-[9.5 Leveraging Randomness with Math.random()](#95-leveraging-randomness-with-mathrandom)
72
72
-[9.6 Persisting Data with Web Storage](#96-persisting-data-with-web-storage)
73
+
-[9.7 Coding Logic vs Data Retrieval & Execution](97-coding-logic-vs-data-retrival--execution)
73
74
74
75
## Tips and Best Practices
75
76
@@ -223,11 +224,11 @@ Examples illustrating syntax for variable declarations and data types.
223
224
224
225
## 3.1 FUNCTIONS
225
226
226
-
Detailed exploration of functions in JavaScript, including parameters and advanced concepts.
227
+
In JavaScript, functions are fundamental building blocks, acting as callable objects to perform tasks or return values. They enhance code organization, reusability, and testability. This section delves into functions, covering declarations, parameters, and advanced concepts.
227
228
228
229
### Function Declarations
229
230
230
-
Standard way to define a function with parameters.
231
+
Define functions using the `function` keyword, specifying parameters within parentheses.
231
232
232
233
```javascript
233
234
functiongreet(name) {
@@ -238,9 +239,9 @@ console.log(greet('Alice'));
238
239
239
240
Outputs "Hello, Alice!"
240
241
241
-
### Function Parameters
242
+
### Function Parameters and Arguments
242
243
243
-
Functions can take parameters as input.
244
+
Functions accept inputs called parameters, defined at declaration, and are placeholders for data passed during invocation, called arguments. Parameters are locally scoped within the function, allowing you to reference them even if they haven't been assigned outside. Additionally, you can reference external or existing parameters within the function body. Arguments, on the other hand, are the actual values passed to the function when it's called, allowing for dynamic operation based on the values provided.
ES6 allows default values for parameters, used when no argument is provided for that parameter.
255
256
256
257
```javascript
257
258
functionsay(message='Hi') {
@@ -263,7 +264,7 @@ function say(message = 'Hi') {
263
264
264
265
### Rest Parameters (ES6)
265
266
266
-
Handle an indefinite number of parameters.
267
+
Rest parameters syntax (...) enables functions to accept an indefinite number of arguments as an array.
267
268
268
269
```javascript
269
270
functionsumAll(...numbers) {
@@ -276,20 +277,41 @@ Outputs 6
276
277
277
278
### Arrow Functions (ES6)
278
279
279
-
Concise way to write functions:
280
+
Arrow functions offer a concise syntax for writing functions, useful for short operations and as function arguments.
280
281
281
-
- (parameters) => expression
282
+
```javascript
283
+
constarrowFunction= (parameters) => expression;
284
+
```
282
285
283
286
```javascript
284
-
constmultiply= (x, y) => x \* y;
285
-
console.log(multiply(2, 3));
287
+
// Traditional function expression
288
+
functionadd(a, b) {
289
+
return a + b;
290
+
}
291
+
292
+
// Arrow function expression
293
+
constaddArrow= (a, b) => a + b;
294
+
295
+
console.log(add(2, 3)); // Output: 5
296
+
console.log(addArrow(2, 3)); // Output: 5
286
297
```
287
298
288
-
Outputs 6
299
+
In this example, the arrow function addArrow accomplishes the same thing but with a more concise syntax. It omits the function keyword and uses the => arrow syntax to define the function.
300
+
301
+
### Function Expressions
302
+
303
+
Function expressions assign an anonymous function to a variable. They offer flexibility, allowing functions to be defined and passed as data. Within these function expressions, parameters can refer to both existing variables and parameters that have not been previously declared or defined. This allows for dynamic behavior based on the values passed to the function when it's invoked.
304
+
305
+
```javascript
306
+
constsquare=function (x) {
307
+
return x * x;
308
+
};
309
+
console.log(square(4)); // Outputs 16
310
+
```
289
311
290
312
### IIFE (Immediately Invoked Function Expression)
291
313
292
-
Function that runs as soon as it is defined.
314
+
IIFEs are functions that execute immediately upon definition, useful for initializing applications or namespaces.
293
315
294
316
```javascript
295
317
(function () {
@@ -299,7 +321,7 @@ Function that runs as soon as it is defined.
299
321
300
322
### Higher-Order Functions
301
323
302
-
Functions that take or return other functions.
324
+
These functions accept or return other functions, facilitating abstraction and composition in programming.
303
325
304
326
```javascript
305
327
functionapplyOperation(a, b, operation) {
@@ -371,6 +393,21 @@ for (const number of numbers) {
371
393
}
372
394
```
373
395
396
+
### Ternary Operator
397
+
398
+
The ternary operator is a concise way to write conditional statements in JavaScript. It takes three operands: a condition, an expression to execute if the condition is true, and an expression to execute if the condition is false.
399
+
400
+
```javascript
401
+
condition ? expression1 : expression2;
402
+
```
403
+
404
+
```javascript
405
+
constage=20;
406
+
conststatus= age >=18?'adult':'minor';
407
+
408
+
console.log(status); // Outputs: 'adult' since age is greater than or equal to 18
409
+
```
410
+
374
411
### Array.forEach()
375
412
376
413
Executes a specified function for each element within an array. While it's not a traditional loop, it's closely related to the topic of array iteration.
@@ -425,18 +462,34 @@ if (condition1) {
425
462
426
463
### switch Statement
427
464
428
-
Executes code based on the value of an expression.
465
+
Switch statements provide a way to perform different actions based on different conditions. They allow you to evaluate an expression and execute code blocks based on the matching case. Switch statements offer a more concise alternative to multiple if...else statements when dealing with multiple conditions.
429
466
430
467
```javascript
431
468
switch (expression) {
432
-
case x:
433
-
// Code for case x
469
+
case value1:
470
+
// Code block to execute if expression equals value1
471
+
break;
472
+
case value2:
473
+
// Code block to execute if expression equals value2
474
+
break;
475
+
// Additional cases as needed
476
+
default:
477
+
// Code block to execute if expression doesn't match any case
478
+
}
479
+
```
480
+
481
+
```javascript
482
+
constday='Monday';
483
+
484
+
switch (day) {
485
+
case'Monday':
486
+
console.log('Today is Monday');
434
487
break;
435
-
casey:
436
-
// Code for case y
488
+
case'Tuesday':
489
+
console.log('Today is Tuesday');
437
490
break;
438
491
default:
439
-
// Default code if none of the above cases are true
492
+
console.log("It's neither Monday nor Tuesday");
440
493
}
441
494
```
442
495
@@ -2083,6 +2136,46 @@ Web Storage, encompassing both localStorage and sessionStorage, offers robust so
2083
2136
2084
2137
## [🔝 Back to Top](#top)
2085
2138
2139
+
## 9.7 Coding Logic vs Data Retrieval & Execution
2140
+
2141
+
Understanding the distinction between coding logic and data retrieval & execution in JavaScript is crucial for effective programming.
2142
+
2143
+
### Coding Logic
2144
+
2145
+
Involves the core computational aspects such as algorithms, conditional statements, loops, and functions. It's about the internal logic that doesn't directly interact with the outside world (user interface, external APIs).
2146
+
2147
+
-**Mathematical calculations:** Performing operations like addition, subtraction, multiplication, and division.
2148
+
-**String manipulation:** Operations like concatenation, slicing, or pattern matching.
2149
+
-**Conditional checks:** Making decisions based on certain conditions using if-else statements or switch cases.
2150
+
2151
+
### Data Retrieval and Execution
2152
+
2153
+
Focuses on interacting with external data sources (APIs, databases) and the user interface. It involves fetching, displaying, and reacting to data.
2154
+
Includes fetching API data, handling user inputs, updating the web page dynamically.
Utilized within functions to output a value back to the caller. It serves as an internal mechanism for passing data between functions or parts of your code. The return statement does not produce any visible output in the user interface or console. It is purely for internal data flow within the application.
2161
+
2162
+
```javascript
2163
+
functionfunctionName(parameters) {
2164
+
// Function body
2165
+
// Compute value or perform operations
2166
+
2167
+
return value; // Value to be returned
2168
+
}
2169
+
```
2170
+
2171
+
### When to use various methods of data execution
2172
+
2173
+
-**console.log:** Outputs debugging information to the browser's console, external to application logic and UI.
2174
+
-**alert:** Displays a message to the user via a dialog box, directly interacting with the user, external to code logic.
2175
+
-**.innerHTML/.innerText:** Changes an element's HTML/Text content, directly modifying the web page's visible content.
2176
+
2177
+
## [🔝 Back to Top](#top)
2178
+
2086
2179
## 10.1 TIPS AND BEST PRACTICES
2087
2180
2088
2181
Tips for cleaner, more efficient and maintainable JavaScript code.
@@ -2121,17 +2214,23 @@ Tips for cleaner, more efficient and maintainable JavaScript code.
2121
2214
- Be mindful of loop performance when dealing with large data sets.
2122
2215
- Use efficient loop constructs and consider optimizations like loop unrolling.
2123
2216
2124
-
### 8. Modular Code
2217
+
### 8. Strict Mode
2218
+
2219
+
- Strict mode is a feature in JavaScript that introduces stricter rules for writing JavaScript code.
2220
+
- It helps to prevent common coding errors and makes it easier to write secure JavaScript.
2221
+
- To enable strict mode, add the text `'use strict';` at the beginning of a script or a function body
2222
+
2223
+
### 9. Modular Code
2125
2224
2126
2225
- Break down your code into reusable and modular components or functions.
2127
2226
- Embrace the concept of modules and imports for better code organization.
2128
2227
2129
-
### 9. Testing
2228
+
### 10. Testing
2130
2229
2131
2230
- Write unit tests to ensure the correctness of your code.
2132
2231
- Explore testing frameworks like Jest, Mocha, or Jasmine.
2133
2232
2134
-
### 10. Stay Updated
2233
+
### 11. Stay Updated
2135
2234
2136
2235
- Keep up with the latest developments in JavaScript and web technologies.
2137
2236
- Follow industry best practices and consider performance optimizations.
0 commit comments