Hello, @code0542734588! Welcome to the forum.
While this simplistic example may not be very practical, it does show how to use a helper function. If you had several functions that all needed to perform a similar mathematical calculation, and then each function additionally performed its own unique operations with the returned value, this type of helper function becomes very useful to “DRY” your code.
Hi this is about ‘helper functions’ in ‘introduction to javascript’ here In the exercise it
asks to create a helper function to find the total cost of monitors. However at the end it makes the user define the a const variable totalCost and then log it to make sure that it works. I was however wondering why it asks to define a variable and why not just print console.log(costOfMonitors (5,4));
This is all very fine and good if we just wish to confirm the functions work, but what if we wish to use the return value in further computations? A variable is way we can do that.
This exercise talks about using helper functions as a way to clean up functions or to break down large logical tasks into smaller manageable tasks. There’s no question that helper functions make the code more readable but out of idle curiosity: is the separation of functions into helper strictly more efficient from a writing perspective or also from a computing perspective? Or would a computer run a large logical task more efficiently if it was a single function? I know the difference is undoubtedly negligible in terms of how fast we perceive it to be running, but I’m just curious.
Helper functions serve single purpose utility, but as standalone objects they can be shared with the rest of the program or included in a module that can be shared with many programs.