javascript ,Reactjs, nodejs,vuejs
How to create JS Functions
- something about function
Function means "Building Blocks" of the program . It can allow to reuse the code without repetition. For Example , we need to create shopping cart . To get AddItemCart , total-amount . It is used for the program .
Let's talk about function . How to create Functions.
Function Declaration:
function Createfunction(){
alert("Hello world!");
}
Createfunction();
Let's explain the code . "function" is keyword which have to write first then I create function Name which can be wrote any-letter and The parameters will be the brackets(empty in the example above) . Finally, Function body code where you have to write your all codes .
Createfunction() is called for function output . If you write two times, the output will show two times . it executes the code of the functions.
Local variable:
Local variable declared inside the function body is only visible that function . Make sure that when You will write the title of function name , You can write the first letter of word can be capital letter because it will help you to see look nice .. Example:CreateMyName. In addition, I will write later how to write properly the name of function.
Example:1
function CreateMyName(){
var name = "poran";
alert(name);
}
CreateMyName(); // output : poran
alert(name); // Error output because Variables is called as a local .
outer Variable:
outer variable declared outside of the function .
Example:2
var name = "poran";
function ShowName(){
var nickName = "Shaha" + name;
alert(nickName);
}
ShowName(); // output : Shahaporan
Outer variable is worked as global variables.It has full accessed and modify in the code .
Example:3
var name3 = "poran"; // outerVariable
function ShowName(){
name3 = "Sujon"; // Outer variable is changed .
var nickName = "My Name is" + name3;
alert(nickName);
}
alert(name3); // Show outer value
ShowName(); // output : My Name is Shahaporan
alert(name3) // Show changed value
Here is missing something about outer variables that I will write next articles .
"Happy coding"
Top comments (0)