Why Functions are special in javascript?
Function scope Function hoisting calling a function Function declaration Function Expression define a funcion What is function? lexical scope
Photo by Nick Fewings on Unsplash
What is a function in javascript?
A function is like a procedure in which a set of statements perform a task or calculate a value by taking some input. A function returns some output. Input and output become related to one another in some obvious relationship. You must define a function somewhere in the scope for function calling. A method that is a property of an object is a function.
How to define a function in javascript?
There are two ways to define a function-Function declaration and Function expression.
Function declarations
It consists a 'function' keyword, followed by
The name of the function.
A list of parameters to the function, enclosed in parantheses and separated by commas.
The javascript statements that define the function, enclosed in curly brackets, {/* */}.
Example
function cube(number) {
return number*number*number;
}
Here function
is a keyword and cube
is the function name. It is taking one parameter number
. It returns with number*number*number
.
Passing an object as a parameter to the function
When we pass an object as a parameter of the function and the function changes the object value, then the change is visible outside the function. It is shown in this example.
//function declaration
function myFun(theObject) {
//code for object value change
theObject.make="Toyota";
}
//define object
const myCar={
make:"Honda",
model: "Accord"
};
//property change by function
myFun(myCar);
Function Expressions
Functions also can be created by a function expression.
A function without name
If a function does not have to have a name, it can be anonymous.
//function expression
const cube = function (number) {
return number*number*number;
};
const x=cube(3); //x gets the value 27
A function with name
const factorial = function fac (n) {
return n<2? 1 : n*fac(n-1);
};
console.log(factorial(3)); //log : 6
How to call a function in javascrit?
If a function is define like this.
function square (n) {
return n*n;
};
If you want to call this function, you have to write the function name with its parameter criteria like this.
square (5); //it will reutrn a value 25.
A function can call itself?
Here is a function of factorial.
function factorial (n) {
if (n===0||n===1) {
return 1;
} else {
return n*factorial(n-1);
}
}
In this function, return value is calling function itself.
factorial (3); //it will return 6
A function can also call dynamically. You can see in next blog.
What is function hoisting?
In a coding area, if a function is declared after the function calling, the function call outputs the value without no error. The Javascript interpreter hoists the entire function declaration to the top of the current scope.
Function hoisting only works with function declarations-not with function expressions.
See example for better understanding.
console.log(square(5)); //25
function square(n) {
return n*n;
}
In this code, the function is called first, then funtion is declare.
This is because The Javascript interpreter hoists the entire function declaration to the top of the current scope, so the code above is equivalent to:
//All function declarations are effectively at the top of the scope
function square (n) {
return n*n;
}
console.log(square(5));//25
What is function scope?
A function scope is the scope in which all variables is accessed by that function. A function can access all variables and functions defined inside the scope in which it is defined. The variables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function.
In other words, a function defined in the global scope can access all variables defined in the global scope.
// variables are defined in the global scope.
const num1=20;
const num2=3;
const name="Chamak";
//This function is defined in the global scope
function multiply( ) {
return num1*num2;
}
multiply( ); //returns 60
A function defined inside another function can also access all variables defined in its parent function and any other variables to which the parent function has access. This type of scope is called lexical scope.