Skill Making

Always Increase Your Knowledge

Use of Functions in JavaScript | Use of JavaScript With HTML File

Use of Functions in JavaScript | Use of JavaScript With HTML File

 

In this article, you will learn about the use of functions in JavaScript. If you are also interested to learn about this topic then you should read this article till the end. Here I will explain you this topic with the help of an example.




Picture of use of functions in JavaScript

What is the function in JavaScript?

How to use functions in JavaScript?

How to write a code of functions in JavaScript?

If you have also these questions then you are at right place. Here you will get the right information regarding this topic. I hope you will love this article.

You should also read them:

Control Statements in JavaScript

Use of Switch Case in JavaScript




If you want to boost your JavaScript programming knowledge then you must read this book. It is really a wonderful weapon to learn JavaScript.

Functions in JavaScript

  • Functions are a block of code designed to perform a certain action.
    • function functionName(variableList){
      
      }
  • For Example, You want to add a separate functionality in your webpage. Like you want to convert Kg in gm. Then you will write some code of JavaScript in a separate block. Or you want to calculate the percentage of a product then you will write some code of JavaScript in a block. Thus you can write many types of code in different blocks.
  • It means you want to do many tasks in a single webpage then you will give a separate block for a separate task. These blocks are called a function. And you can use them at a different time in different places.
  • And we need to give a separate name for a single block of function.
  • Here you need to write a keyword “function”, before the name of the function. And you need to write it in small letters.

Function Execution

  • The function is executed when
    • The function is invoked from another JS function.
    • The function is attached to some event and event occurs.

Why Function

  • You can reuse code.
  • The script inside the function is not executed until the function is invoked.

Variables in Functions

  • Local Variable
  • Global Variable

Local Variable

When you will create a variable under a function then it called a local variable. The variable’s functionality will work on the body of that function. You can create a variable by write “var” keyword. You cannot use local variable out of that function’s body.




For Example:

<script>

Function hello(){

Var x;

}

</script>

Global Variable

If you want to create a global variable then you will not write a “var” keyword while declaring a variable. You can use a global variable in your whole webpage.

For Example:

<script>

Function hello(){

Y=2;

}

</script>

Example:

<!DOCTYPE html>

<html>

<head>

<title>Functions in JavaScript</title>

</head>

<body>

<script type="text/javascript">




function skillmaking()

{

document.write("Welcome in SKILL MAKING");

}

</script>

<h1>

Welcome

</h1>

<script type="text/javascript">

skillmaking();

</script>

</body>

</html>

The output of The use of Functions in JavaScript:

Picture of use of functions in JavaScript




Conclusion

Finally, you got information about the use of functions in JavaScript. I hope you got the information which you want. If you find this article useful then please share it with your friends. Thank you for reading this article till the end.

Leave a Reply

Your email address will not be published. Required fields are marked *

Skill Making © 2018 Frontier Theme