Kay Ashaolu
Text
// Function Declaration
function fetchData(url) {
// Fetch data from API
}
// Function Expression
const fetchData = function(url) {
// Fetch data from API
};
// Arrow Function
const fetchData = (url) => {
// Fetch data from API
};
if
, for
, { }
)Importance in Web Architecture:
// Module Pattern using IIFE
const App = (function() {
let privateData = 'secret';
return {
getData: function() {
return privateData;
},
};
})();
console.log(App.getData()); // Output: 'secret'
Implications:
Relevance to Web Architecture:
function createCounter() {
let count = 0;
return function() {
count += 1;
return count;
};
}
const counter = createCounter();
counter(); // Returns: 1
counter(); // Returns: 2
Arrow Functions:
this
bindingLet and Const:
const
Modules:
import
and export
for code organizationDesign Patterns:
Frameworks and Libraries:
Best Practices:
Key Takeaways:
Further Exploration:
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
if (userIsLoggedIn) {
displayDashboard();
}
Else-If Syntax:
if (condition1) {
// code for condition1
} else if (condition2) {
// code for condition2
} else {
// code if none of the above conditions are true
}
Nested If Statements:
if (outerCondition) {
if (innerCondition) {
// code for inner condition
}
}
Simplifies complex conditional logic Syntax:
switch (expression) {
case value1:
// code for case value1
break;
case value2:
// code for case value2
break;
default:
// code if no cases match
}
Example:
switch (userRole) {
case 'admin':
accessLevel = 'full';
break;
case 'editor':
accessLevel = 'partial';
break;
default:
accessLevel = 'read-only';
}
false
0
''
(empty string)null
undefined
NaN
All values that are not falsy Example:
if ('') {
// This block will not execute
}
if ('hello') {
// This block will execute
}
true
or false
AND (&&
):
if (condition1 && condition2) {
// code executes if both conditions are true
}
OR (||
):
if (condition1 || condition2) {
// code executes if at least one condition is true
}
NOT (!
):
if (!condition) {
// code executes if condition is false
}
OR Assignment (||=
):
x ||= y; // Equivalent to: x = x || y;
AND Assignment (&&=
):
x &&= y; // Equivalent to: x = x && y;
Nullish Coalescing Assignment (??=
):
x ??= y; // Equivalent to: x = x ?? y;
Useful for setting default values or conditions
Shorthand for simple if-else statements Syntax:
condition ? expressionIfTrue : expressionIfFalse;
Example:
const access = age >= 18 ? 'Granted' : 'Denied';
Nested Ternary (use with caution):
const status = score > 90 ? 'A' : score > 80 ? 'B' : 'C';
Makes code more concise but can reduce readability
Function to perform basic arithmetic operations:
function calculator(num1, num2, operator) {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
return 'Invalid operator';
}
}
Usage Examples:
calculator(5, 2, '+'); // Returns 7
calculator(5, 2, '-'); // Returns 3
calculator(5, 2, '*'); // Returns 10
calculator(5, 2, '/'); // Returns 2.5
Demonstrates practical use of switch statements and control flow
for
LoopStandard looping structure in JavaScript Syntax:
for (initialization; condition; increment) {
// code block to execute
}
Example:
for (let i = 0; i < 5; i++) {
console.log(`Iteration ${i}`);
}
break
: Exit the loop immediately
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i);
}
continue
: Skip the current iteration
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) continue;
console.log(i); // Logs odd numbers
}
while
and do...while
Loopswhile
loop: Executes as long as the condition is true
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
do...while
loop: Executes code block at least once
let count = 0;
do {
console.log(count);
count++;
} while (count < 5);
forEach
MethodExecutes a provided function once for each array element Syntax:
array.forEach((element, index, array) => {
// code to execute
});
Example:
const colors = ['red', 'green', 'blue'];
colors.forEach(color => {
console.log(color);
});
map
MethodCreates a new array by applying a function to each element Syntax:
const newArray = array.map(element => {
// return new value
});
Example:
const numbers = [1, 2, 3];
const squares = numbers.map(number => number * number);
filter
MethodCreates a new array with elements that pass a test Syntax:
const filteredArray = array.filter(element => {
// return condition
});
Example:
const ages = [18, 21, 16, 25];
const adults = ages.filter(age => age >= 18);
reduce
MethodReduces an array to a single value Syntax:
const result = array.reduce((accumulator, currentValue) => {
// return updated accumulator
}, initialValue);
Example:
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((total, number) => total + number, 0);