Member-only story
Learning to code often starts small — really small — like adding two numbers together. Though it may seem trivial, mastering these building block concepts lays the foundation for tackling more complex programming challenges down the road.
In this quick tutorial, we’ll create a simple Javascript function that takes two number parameters and returns their sum. Ready to level up your basic math skills? Let’s get started!
Defining the Function
First we need to define our function, including the name and parameters it will accept:
function addNumbers(num1, num2) {
}
The function is called addNumbers. By adding num1 and num2 between parentheses, we’ve defined them as parameters that will be passed when calling the function.
Adding the Numbers
Next we perform the actual addition and return the sum:
function addNumbers(num1, num2) {
var sum = num1 + num2;
return sum;
}
The num1 and num2 parameters are treated just like regular variables. We create a new variable called sum that stores the result of num1 + num2. Finally return sum passes that…