An overview of Javascript fundementals
A function is a stored set of statements that perform some tasks or does some computation and then return the result to the user. Once stored the function can then be called whenever it is needed instead of rewriting all those statements. It can also be set to take input values.
The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can call that function.
JavaScript has many built-in functions that we can call to perform certain tasks. We can also create our own user-defined functions in JavaScript using the keyword function. This is the basic syntax to create a function in JavaScript:
function functionName(inputParameter1, inputParameter2, ...) {
//function statements
}
//this function doubles the input value function doubleNumber(number) { return number * 2; } //here we call the function console.log(doubleNumber(10)); //outputs 20
Here are some common built-in functions:
Create a function called 'add' that adds 2 numbers and returns the result.
add(num1, num2) { let result = num1 ; return (); } try Again!