• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Functions in JavaScript

JavaScript Functions

A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing same code again and again. This will help programmers to write modular code. You can divide your big program in a number of small and manageable functions.

Like any other advance programming language. JavaScript also supports all the features necessary to write modular code using functions.

You must have seen functions like alert() and write() in previous chapters. We are using these function again and again but they have been written in core JavaScript only once.

JavaScript allows us to write our own functions as well. This section will explains you how to write your own functions in JavaScript.

Function Definition

Before we use a function we need to define that function. The most common way to defines a function in JavaScript is by using the function keyword. followed by a unique function name, a list of programmers(that might be empty) and a statement surrounded by curly braces. The basic syntax is shown here:

<script type=”text/javascript”>

<!—

functionfuncitonname (parameter-list)

{

statements

}

//- – >

</script>
Example 

A simple function that takes no parameters called sayHello is defined here:

<script type=”text/javascript”>

<!—

functionsayHello()

{

alert(“Hello there”);

}

//- – >

</script>

Calling a function 

To invoke a function somewhere later in the script, you would simple need to write the name of that function as follows:

<script type=”text/javascript”>

<!—

sayHello();

// – ->

</script>

Function Parameters 

Till now we have seen function without a parameters. But there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters.

A function can take multiple parameters seperated by comma.

Example 

Let us do a bit modification in our sayHello function. This time it will take two parameters.

<script type=”text/javascript”>

<!—

functionsayHello(name,age)

{

alert(name + “is” + age + “years old.”);

}

// – ->

</script>

Note

 We are using + operator to concatenate string and number string and number all together. JavaScript does not mind in adding number into strings.

Now we can call this function as follows :

<script type=”text/javascript”>

<!—

sayHello(‘Zara’,7);

// – ->

</script>

The return statement

A JavaScript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function.

For example you can pass two numbers in a function and then you can expect from the function to return their multiplication in your calling program.

Example

This function takes two parameters and concatenates them and return resultant in the calling program:

<script type=”text/javascript”>

<!—

function concatenate(first, last)

{

var full ;

full =first + last;

return full ;

}

// – – >

</script>

Now we can call this function as follows 

<script type = “text/java script”>

<!- –

var result;

result=concatenate(‘Zara’,’Ali’);

alert(result);

// — >

</script>
fukk=first+last;

return full;

}

// – – >

</script>

 

Now we can call this function as follows 

<script   type=”text/javascript”>

<!—

var result;

result= concatenate(‘Zara’,’Ali’);

alert(result);

// – –

</script>

JavaScript special characters

In JavaScript  you can add special charactenrs to a text string by using the backslash sign.

Insert special characters

The backslash(/) is used to insert apostrophes, new line, quotes, and other special characters into a text string.

Look at the following JavaScript code:

Var txt=”We are the so-called ” Vikings “ from the north;

document.write(txt);

In JavaScript, a string is started and stopped with either single or double quotes. This means that the string above will be chopped to : We are the so-called.

To solve this problem, you must place a backslash (/) before each double quote in “Viking”. This turns each double quote into a string literal.

Var txt=”We are the so-called  \”Viking\” from the north”;

document.write(txt);

JavaScript will now output the proper text string : We are the so-called “Vikings” from the north

The table below lists other special characters that can be added to a text string with the backslash sign:

Code Outputs
\ single Quote
\” double quote
\\ Backslash
\n new line
\r Carriage return
\t Tab
\b Backspace
\f form feed.

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Functions in JavaScript"

Leave a Message

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

Site Disclaimer, Copyright © 2016 - All Rights Reserved.