Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
1. JavaScript: Basics
9Topics
|
10 Quizzes
2. JavaScript: Variables
9Topics
|
10 Quizzes
3. JavaScript: Conditional Statements
10Topics
|
11 Quizzes
4. JavaScript: Functions
10Topics
|
11 Quizzes
5. JavaScript: Scope
6Topics
|
7 Quizzes
6. JavaScript: Arrays
11Topics
|
12 Quizzes
7. JavaScript: Loops
8Topics
|
9 Quizzes
8. JavaScript: High Order Functions
3Topics
|
3 Quizzes

Participants 24

In Progress
Lesson 4, Topic 7
In Progress

JavaScript: Helper Functions

Yasin Cakal 7 Nov 2021
Lesson Progress
0% Complete

A function’s return value can also be used inside another function. These functions that are called within another function are usually referred to as helper functions. Because each function performs a specific task, our code is easier to read and debug if needed.

We could write two functions to count computers in the lab and calculate their cost.

``````function monitorCount(rows, columns) {
return rows * columns;
}

function costOfMonitors(rows, columns) {
return monitorCount(rows, columns)  * 300;
}

const totalCost = costOfMonitors(4,5);
console.log(totalCost);

//Output: 6000``````
• Firstly we created a `function monitorCount(rows,columns)` to determine the number of monitors to order for an office. It takes `rows` and `columns` as parameters and returns the number of monitors needed by multiplying `rows*columns`.
• Then we wrote another function that calculates the price using the `monitorCount(rows,columns)`. We declared a `costOfMonitors(rows,columns)` function with two parameters, the first of which is `rows` and the second of which is `columns`.
• With in the `costOfMonitors(rows,columns)` we called `monitorCount(rows,columns)`, `monitorCount(rows,columns)` will return monitor count that will then get multiplied by 300 and will return the value.
• The cost should be saved as a variable. So we used the `const` keyword to declare a variable named `totalCost`. Assign the result of calling `costOfMonitors()` with the arguments `4` and `5` to `totalCost`.

We can use functions to divide up small chunks of logic or tasks and then use them as needed. Helper functions can be used to break down large, difficult tasks into smaller and more manageable tasks.

Quizzes