TechMentorAI

Function declaration

in JavaScript

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.

Imagine Function declaration in JavaScript

How to declare a function?

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
}

Usage of function

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);

Example of Function Declaration in JavaScript

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

No Time to Read? Learn on the Go!

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.

Imagine Function declaration in JavaScript

How to write a function?

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.

Code example

function moveForward() {
  console.log('The robot moves forward.');
}

How to use a function?

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.

Code example

moveForward(); // It will say 'The robot moves forward.'

Why are functions useful?

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.

No Time to Read? Learn on the Go!

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

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email