Skip to content

Commit acfabb6

Browse files
agilan11moT01
andauthored
fix(curriculum): Added missing information to JS function Review page (freeCodeCamp#58384)
Co-authored-by: Tom <[email protected]>
1 parent e60c3f8 commit acfabb6

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

curriculum/challenges/english/25-front-end-development/review-javascript-functions/6723c5b8601a40a100bb59c9.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,72 @@ Review the concepts below to prepare for the upcoming quiz.
1313

1414
- Functions are reusable blocks of code that perform a specific task.
1515
- Functions can be defined using the `function` keyword followed by a name, a list of parameters, and a block of code that performs the task.
16+
17+
```js
18+
function addNumbers(x, y, z) {
19+
return x + y + z;
20+
}
21+
22+
console.log(addNumbers(5, 3, 8)); // Output: 16
23+
```
24+
1625
- Arguments are values passed to a function when it is called.
26+
- A function call is the process of executing a function in a program by specifying the function's name followed by parentheses, optionally including arguments inside the parentheses.
1727
- When a function finishes its execution, it will always return a value.
1828
- By default, the return value of a function is `undefined`.
1929
- The `return` keyword is used to specify the value to be returned from the function and ends the function execution.
30+
- Default parameters allow functions to have predefined values that will be used if an argument is not provided when the function is called. This makes functions more flexible and prevents errors in cases where certain arguments might be omitted.
31+
32+
```js
33+
const calculateTotal = (amount, taxRate = 0.05) => {
34+
return amount + (amount * taxRate);
35+
};
36+
37+
console.log(calculateTotal(100)); // Output: 105
38+
```
39+
40+
- Function Expressions are functions that you assign to variables. By doing this, you can use the function in any part of your code where the variable is accessible.
41+
42+
```js
43+
const multiplyNumbers = function(firstNumber, secondNumber) {
44+
return firstNumber * secondNumber;
45+
};
46+
47+
console.log(multiplyNumbers(4, 5)); // Output: 20
48+
```
2049

2150
## Arrow Functions
2251

2352
- Arrow functions are a more concise way to write functions in JavaScript.
24-
- Arrow functions are defined using the `=>` syntax between the parameters and the function body.
53+
54+
```js
55+
const calculateArea = (length, width) => {
56+
const area = length * width;
57+
return `The area of the rectangle is ${area} square units.`;
58+
};
59+
60+
console.log(calculateArea(5, 10)); // Output: "The area of the rectangle is 50 square units."
61+
```
62+
2563
- When defining an arrow function, you do not need the `function` keyword.
2664
- If you are using a single parameter, you can omit the parentheses around the parameter list.
65+
66+
```js
67+
const cube = x => {
68+
return x * x * x;
69+
};
70+
71+
console.log(cube(3)); // Output: 27
72+
```
73+
2774
- If the function body consists of a single expression, you can omit the curly braces and the `return` keyword.
2875

76+
```js
77+
const square = number => number * number;
78+
79+
console.log(square(5)); // Output: 25
80+
```
81+
2982
## Scope in Programming
3083

3184
- **Global scope**: This is the outermost scope in JavaScript. Variables declared in the global scope are accessible from anywhere in the code and are called global variables.

0 commit comments

Comments
 (0)