JavaScript Functions
Last weeks I've worked mostly with arrays and functions, and I want to share some of what I've learned about functions.
JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it). Functions are used to perform operations. We can call JavaScript function many times to reuse the code. There are mainly two advantages of JavaScript functions:
JavaScript Function Syntax
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...). The code to be executed, by the function, is placed inside curly brackets: {}.
function functionName(parameter1, parameter2, parameter3){
//code to be executed
}
Function parameters are listed inside the parentheses () in the function definition. Function arguments are the values received by the function when it is invoked. Inside the function, the arguments (the parameters) behave as local variables.
Function Invocation
As I said above, the code inside the function will execute when "something" invokes (calls) the function:
The () operator invokes the function. Accessing a function without () will return the function object instead of the function result.
JavaScript Functions can have 0 or more arguments(parameters).
A simple example of function in JavaScript that does not has arguments:
function myFunction() {
alert("Hi! This is a simple JS function");
}
myFunction()
//Output will show an alert in browser with the message above.
JavaScript Function Arguments
We can call function by passing arguments. Let’s see the example of function that has one argument:
let number = 4;
function getCube(number) {
alert(number * number * number)
}
get cube();
//Output will show an alert with the number 64 (4 * 4 * 4)
Function with Return Value
We can call function that returns a value and use it in our program. Let’s see the example of function that returns value:
function getInfo() {
return "Hello JavaScript!"
}
console.log(getInfo());
//Output will show in console: Hello JavaScript!
Recommended by LinkedIn
JavaScript Function Object
In JavaScript, the purpose of Function constructor is to create a new Function object. It executes the code globally. However, if we call the constructor directly, a function is created dynamically but in an unsecured way.
Syntax:
new function([arg1[,arg2[,...argn]],]functionBody)
Parameter:
arg1, arg2, .... , argn - represents the argument used by function.
functionBody - represents the function definition.
JavaScript Function Methods
Let's see function ,ethods with description:
JavaScript Function Object Examples
Example for displaying the sum of given numbers:
let add = new Function('num1', 'num2', 'return num1 + num2');
console.log(add(2, 5));
//Output: 7
Example for displaying the power of provided value:
let pwr = new Function('num1', 'num2', 'return Math.pow(num1,num2)')
console.log(pwr(2, 4));
//Output: 16
* The Math.pow() method returns the value of a base raised to a power. That is:
Why Functions?
You can reuse code: define the code once, and use it many times. You can use the same code many times with different arguments, to produce different results.