JavaScript - Functions
- Introduction to JavaScript functions
- JavaScript built-in functions
- Defining JavaScript functions
- Call JavaScript functions
By using functions in JavaScript you
can use any code repeatedly in many places in the program and you can avoid the problem of writing the same code many times.
This increases the reusability of
the code, that is, you can apply the same code to many places in the program as
per the requirement. Functions make your program more structured and readable.
JavaScript provides some built-in
functions that you can use in your program. These functions perform some
regular basic tasks. Let's try to know about these
functions.
JavaScript Built-in Functions
There are five built-in functions in JavaScript
that you can access and use according to your requirements.
eval ()
This function evaluates the passed string.
If a string is a number then returns it, otherwise, it gets terminated.
parseInt ()
In this function, two parameters are
passed. The first parameter is a string and the second parameter is an integer
that represents the base such as 10 or 16. It returns a number of that base
according to the function string.
parseFloat ()
This function reads a string and if a string
is a float then this function returns it.
escape ()
This function returns a string by
converting it into another character. Such as at the place of space it returns +
sign and at the place of alphanumeric values, it returns %.
unescape ()
By this function the result of the
escape () function can be converted back into the original.
Defining JavaScript Functions
It's easy to define functions in
JavaScript. For this, you use a function keyword. After this keyword, you type the
name of the function and then define the list of parameters.
The structure of the function is
given below.
Syntax
function functionName(parameters_ list)
{
// your code will be here.
}
You can create any JavaScript
function on the basis of this structure. An example of creating functions in
JavaScript is being given below.
<script type="text/javascript">
function hello()
{
document.write("Hello, how are you?");
}
</script>
Call JavaScript Function
After defining the function, you have
to call it to use. You can call Function anytime in the entire script. For this, you write the name of the function thereafter putting the brackets and end with a semicolon. An example is being given below.
<! - JavaScriptFunctionsExample.html
->
<!DOCTYPE html>
<html>
<head>
<title> javascript function calling example </title>
<script type="text/javascript">
// Defining function
function hello()
{
document.write("Hello! How are you?");
}
</script>
</head>
<body>
<script type="text/javascript">
// Calling function
hello();
</script>
<head>
<title> javascript function calling example </title>
<script type="text/javascript">
// Defining function
function hello()
{
document.write("Hello! How are you?");
}
</script>
</head>
<body>
<script type="text/javascript">
// Calling function
hello();
</script>
</body>
</html>
Hello! How are you?
JavaScript Function Parameters
If you want, you can pass a value to
the function and the function can evaluate this value and show the output. You
define the parameter in brackets to pass a value to the function. To define the
parameter, you name the variable that will hold the value. An example is being given
below.
<script type="text/javascript">
function hello (name) //Here name is a parameter
{
document.write("Hello" + name);
}
</script>
After defining the parameter, when
you call the function, you pass a value for that parameter. By doing so, it
gets the value function and the function execution on it. An example is given
below.
<! - JavaScriptParameterFunctionExample.html ->
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Parameter Example </title>
<script type="text/javascript">//
function greeting(name)
{
document.write("Hello friend! How are you? " + name);
}
</script>
</head>
<body>
<script type="text/javascript">
// Calling
greeting('Abdussamad');
</script>
</body>
</html>
The above script will generate the below output.
Hello friend! How are you? Abdussamad
Excellent post, thanks for this. I gathered lots of information from this and I am happy about it. Do share more updates.
ReplyDeletePHP Training in Chennai
Best PHP Training Institute in Chennai
PHP Institute in Chennai
Angular Training in Chennai
Web Designing Training in Chennai
Salesforce Training in Chennai
Tally course in Chennai
Machine Learning course in Chennai
PHP Training in Velachery
PHP and MYSQL Interview Questions
I never used JavaScript. It may sound very ridiculous, but, I always thought that Java is a game platform. I was so wrong about it.
ReplyDeleteExcellent post, it will be definitely helpful for many people. Keep posting more like this.
ReplyDeleteSalesforce Training in Chennai
Salesforce Course in Chennai
Salesforce Training
AngularJS Training in Chennai
ccna course in Chennai
React JS Training in Chennai
Tally course in Chennai
Tips for Salesforce Professionals
Salesforce Training in Velachery