DEV Community

Elid
Elid

Posted on • Updated on

Functions for beginners

JS functions are a block of code that can be reused anywhere in your code, one of the building blocks of JS the example will be a function declaration.

Example:
// Declaring a function
function twoNumbers(num1 , num2) {
return num1 * num2
}

// Calling the function
twoNumbers(10, 2);

//This code would output 20;

So you see from the example you put in the parameters whatever two numbers you like to put (num1, num2).Then when you are ready to call the function in the argument twoNumbers(10, 2) or whatever two numbers you like output it to the console in your browser.

console.log(twoNumbers(10, 2));

This will output in the console I use chrome so I click ctrl + shift + i

You can do the same in ES6 Arrow Functions which will be much more less code and no return statement needed if is one line.

// Arrow function
const twoNumbers = (num1 , num2) => num1 * num2;
console.log(twoNumbers(10, 2)); //20

Top comments (0)