Loading Please Wait...
JavaScript functions are defined with the function keyword.
You can use a function declaration or a function expression.
Function declaration consist of the keyword function followed by function name and {}.
Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon).
function functionName(parameters) {
// code to be executed
}
function myFunction(a, b) {
return a * b;
}
A JavaScript function can also be defined using an expression. A function expression can be stored in a variable.
After a function expression has been stored in a variable, the variable can be used as a function.
const multiply = function (a, b) {return a * b};
let c = multiply(4, 3);
The function above is actually an anonymous function (a function without a name). Just like the function expression in the above example.
Functions stored in variables do not need function names. They are always invoked (called) using the variable name.
As you have seen in the previous examples, JavaScript functions are defined with the function keyword.
Functions can also be defined with a built-in JavaScript function constructor called Function().
const myFunction = new Function("a", "b", "return a * b");
let x = myFunction(4, 3);
You actually don't have to use the function constructor. The example above is the same as the below.
const myFunction = function (a, b) {return a * b};
let x = myFunction(4, 3);
Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope.
Hoisting applies to variable declarations and to function declarations.
Because of this, JavaScript functions can be called before they are declared.
Functions defined using an expression are not hoisted.
myFunction(5);
function myFunction(y) {
return y * y;
}
Function expressions can be made "self-invoking".
A self-invoking expression is invoked (started) automatically, without being called.
Function expressions will execute automatically if the expression is followed by ().
You cannot self-invoke a function declaration.
You have to add parentheses around the function to indicate that it is a function expression.
(function () {
let x = "Hello!!"; // I will invoke myself
})();
The function above is actually an anonymous self-invoking function (function without name).
JavaScript functions can be used as values or even in an expression.
function myFunction(a, b) {
return a * b;
}
let x = myFunction(4, 3);
let y = myFunction(4, 3) * 2;
The typeof operator in JavaScript returns "function" for functions.
But, JavaScript functions can best be described as objects.
JavaScript functions have both properties and methods.
The arguments.length property returns the number of arguments received when the function was invoked
function myFunction(a, b) {
return arguments.length;
}
let text = myFunction.toString();
Arrow functions allows a short syntax for writing function expressions.
You don't need the function keyword, the return keyword, and the curly brackets.
// ES5
var x = function(x, y) {
return x * y;
}
// ES6
const x = (x, y) => x * y;
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved