Published
A Function declaration in JavaScript is a way to create a function. Think of a function as a small program within a program. It is a block of code that is designed to perform a particular task. Functions get their name from the fact they 'function' as a sub-program. Once the function is defined or 'declared', it can be used anywhere in your program by just calling it according to its name.
To declare a function in JavaScript, we use the keyword 'function', followed by the name you want to give to the function. Then, you define parentheses '()' in which you can put parameters - input the function can work with. After that you use curly brackets '{}' to hold the code that will run when the function is called.
function nameOfFunction(parameter1, parameter2) {
// code to be executed
}
To use or 'call' the function, you simply write the name of the function followed by parentheses '()'. If the function requires parameters, you put your input between these parentheses.
nameOfFunction(value1, value2);
Let's say we want to create a function that adds two numbers together. We might declare that function like this:
function addNumbers(num1, num2) {
return num1 + num2;
}
// Call the function
var sum = addNumbers(5, 3); // sum now holds the value 8
By reading this article, you've invested 1.01 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
Imagine you have a toy robot. You can program it to do certain things like move forward, turn right or left, or pick up toys. In computer programming, we use something called a 'function' like instructions for our robot. When you tell the robot to move forward, for example, you're giving it a function to carry out.
When we write a function in JavaScript, it's kind of like telling our toy robot what to do. We give the function a name, like 'moveForward', and then we tell it what steps to follow. These steps are put inside a set of curly brackets {}, sort of like putting the robot's instructions inside its command center.
function moveForward() {
console.log('The robot moves forward.');
}
Once we have told our robot (or our program) what to do by creating a function, we can then ask it to carry out those instructions whenever we want. We do this by using the function's name, followed by a pair of round brackets (). This is called 'calling' the function.
moveForward(); // It will say 'The robot moves forward.'
Functions are great because they allow us to write a set of instructions once and then use it over and over again. Just like how you don't have to reprogram your toy robot every time you want it to move forward, you don't have to write the same code over and over again if you put it in a function.
By reading this article, you've invested 1.24 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
When working with JavaScript, understanding functions including how to declare them, is crucial. It's also good to understand related topics like Function expressions and Arrow Functions
About author
Roman Y.
Senior Software Engineer at Nike
Certain articles only distributed to subscribers through email