Skip to content

Commit 2aa2161

Browse files
committed
More info on conditional statements, functions.
Logic formulas vs data retrival.
1 parent d3440b8 commit 2aa2161

File tree

1 file changed

+121
-22
lines changed

1 file changed

+121
-22
lines changed

JavaScript-Quick-Reference.md

+121-22
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ A comprehensive yet concise quick-reference and overview of JavaScript fundament
7070
- [9.4 Form Validation](#94-form-validation)
7171
- [9.5 Leveraging Randomness with Math.random()](#95-leveraging-randomness-with-mathrandom)
7272
- [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)
7374

7475
## Tips and Best Practices
7576

@@ -223,11 +224,11 @@ Examples illustrating syntax for variable declarations and data types.
223224

224225
## 3.1 FUNCTIONS
225226

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.
227228

228229
### Function Declarations
229230

230-
Standard way to define a function with parameters.
231+
Define functions using the `function` keyword, specifying parameters within parentheses.
231232

232233
```javascript
233234
function greet(name) {
@@ -238,9 +239,9 @@ console.log(greet('Alice'));
238239

239240
Outputs "Hello, Alice!"
240241

241-
### Function Parameters
242+
### Function Parameters and Arguments
242243

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.
244245

245246
```javascript
246247
function add(a, b) {
@@ -251,7 +252,7 @@ console.log(add(5, 3)); // Outputs 8
251252

252253
### Default Parameters (ES6)
253254

254-
Assign default values to parameters.
255+
ES6 allows default values for parameters, used when no argument is provided for that parameter.
255256

256257
```javascript
257258
function say(message = 'Hi') {
@@ -263,7 +264,7 @@ function say(message = 'Hi') {
263264

264265
### Rest Parameters (ES6)
265266

266-
Handle an indefinite number of parameters.
267+
Rest parameters syntax (...) enables functions to accept an indefinite number of arguments as an array.
267268

268269
```javascript
269270
function sumAll(...numbers) {
@@ -276,20 +277,41 @@ Outputs 6
276277

277278
### Arrow Functions (ES6)
278279

279-
Concise way to write functions:
280+
Arrow functions offer a concise syntax for writing functions, useful for short operations and as function arguments.
280281

281-
- (parameters) => expression
282+
```javascript
283+
const arrowFunction = (parameters) => expression;
284+
```
282285

283286
```javascript
284-
const multiply = (x, y) => x \* y;
285-
console.log(multiply(2, 3));
287+
// Traditional function expression
288+
function add(a, b) {
289+
return a + b;
290+
}
291+
292+
// Arrow function expression
293+
const addArrow = (a, b) => a + b;
294+
295+
console.log(add(2, 3)); // Output: 5
296+
console.log(addArrow(2, 3)); // Output: 5
286297
```
287298

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+
const square = function (x) {
307+
return x * x;
308+
};
309+
console.log(square(4)); // Outputs 16
310+
```
289311

290312
### IIFE (Immediately Invoked Function Expression)
291313

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.
293315

294316
```javascript
295317
(function () {
@@ -299,7 +321,7 @@ Function that runs as soon as it is defined.
299321

300322
### Higher-Order Functions
301323

302-
Functions that take or return other functions.
324+
These functions accept or return other functions, facilitating abstraction and composition in programming.
303325

304326
```javascript
305327
function applyOperation(a, b, operation) {
@@ -371,6 +393,21 @@ for (const number of numbers) {
371393
}
372394
```
373395

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+
const age = 20;
406+
const status = age >= 18 ? 'adult' : 'minor';
407+
408+
console.log(status); // Outputs: 'adult' since age is greater than or equal to 18
409+
```
410+
374411
### Array.forEach()
375412

376413
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) {
425462

426463
### switch Statement
427464

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.
429466

430467
```javascript
431468
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+
const day = 'Monday';
483+
484+
switch (day) {
485+
case 'Monday':
486+
console.log('Today is Monday');
434487
break;
435-
case y:
436-
// Code for case y
488+
case 'Tuesday':
489+
console.log('Today is Tuesday');
437490
break;
438491
default:
439-
// Default code if none of the above cases are true
492+
console.log("It's neither Monday nor Tuesday");
440493
}
441494
```
442495

@@ -2083,6 +2136,46 @@ Web Storage, encompassing both localStorage and sessionStorage, offers robust so
20832136

20842137
## [🔝 Back to Top](#top)
20852138

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.
2155+
2156+
- **Key Methods:** fetch(), .addEventListener(), .innerHTML, console.log, alert.
2157+
2158+
### return
2159+
2160+
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+
function functionName(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+
20862179
## 10.1 TIPS AND BEST PRACTICES
20872180

20882181
Tips for cleaner, more efficient and maintainable JavaScript code.
@@ -2121,17 +2214,23 @@ Tips for cleaner, more efficient and maintainable JavaScript code.
21212214
- Be mindful of loop performance when dealing with large data sets.
21222215
- Use efficient loop constructs and consider optimizations like loop unrolling.
21232216

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
21252224

21262225
- Break down your code into reusable and modular components or functions.
21272226
- Embrace the concept of modules and imports for better code organization.
21282227

2129-
### 9. Testing
2228+
### 10. Testing
21302229

21312230
- Write unit tests to ensure the correctness of your code.
21322231
- Explore testing frameworks like Jest, Mocha, or Jasmine.
21332232

2134-
### 10. Stay Updated
2233+
### 11. Stay Updated
21352234

21362235
- Keep up with the latest developments in JavaScript and web technologies.
21372236
- Follow industry best practices and consider performance optimizations.

0 commit comments

Comments
 (0)