One of the most important topics that we have studied so far is functions
As a developer, you always want your code to be as efficient as possible. You want to make things easier for yourself and others who may be viewing your code
Functions are reusable ‘blocks’ of code that can be called on and used later in the program. Functions are the foundation of JavaScript. They allow you to group instructions together, reuse logic, and make the code or program easier to understand. Each function is created to perform a specific task. You can define a function once and call it whenever you need it
Declaring a Function
Functions can be declared using the function reserved keyword:
function greet(name) {Hello + name!
return;
}
Here, greet is the function name, name is a parameter, and the return statement results in a string
Parameters are placeholders (in the function) for your actual arguments that your will provide later (in the function call). There placeholders can be named anything. In the above example, ‘name’ could have just as easily been ‘apple’. As long as the proper arguments are passed in the function call, everything will still work as it should. Of course, it is a best practice to keep parameter names relevant to the data that is expected
Return vs Undefined
A function always has to return something. If not, the function call will result in undefined
If a function has a return statement, it outputs that value.
function square(num) {
return num * num;
}
function logMessage(msg) {
console.log(msg);
}
console.log(square(4)); // 16
console.log(logMessage("Hi")); // undefined
Calling a Function
Declaring only defines the function. To actually use the function, you must invoke the function by calling it. To call a named function, use its name and add your arguments (not the parameters) enclosed in parenthesis
function greet(name) {Hello + name!
return;
}
console.log(greet(‘sierra’)); // Hello sierra
Arguments must be placed in the function call in the same order as the parameters. The number of arguments and parameters must also be the same
Higher Order Functions
Functions that take a function as an argument or return a function are called higher order functions.
Our biggest focus has always been: .map(), .filter(), .reduce().
const numbers = [1, 2, 3]]
const doubled = numbers.map(n => n * 2) // [2, 4, 6
Recursion is an example of a function returning a function. More specifically, recursion is a function calling itself until a base case is reached. Base cases are extremely important in recursive functions. Without a base case, or without the proper base case, your functions can result in an infinite look and the webpage will crash
function countdown(n) {
// base case
if (n <= 0) return
console.log(n)
countdown(n - 1)
}
countdown(3) // 3, 2, 1
Conclusion
Functions are more than simple code blocks. They are the foundation of a great program. From basic syntax and arrow functions to first-class, pure, and higher-order functions, studying and mastering these concepts will make your code cleaner, more efficient, and easier to maintain.
Top comments (0)