Functions

Javascript Functions

In programming languages, a function is a reusable blocks of code that performs a particular task. Functions are the core of any programming language. Handling functions is an important skill when you learn any programming language. Especially in the case of Javascript, it is one of the most important thing to understand the dynamic nature of this language

Defining functions

Javascript functions are considered as first-class objects, because they can be manipulated like any other object in the language. Javascript functions can have properties and methods, also it can be passed as arguments

Each function in your JavaScript must be addressing it by a unique name for that particular page. Always use meaningful function names, so that when you see it being used later in your programming code. Function names are case sensitive, that is a function name "callMe" is not same as "callme"

Function Declarations:

Syntax

The parameter is optional, you can write function without any parameters.

Function without arguments

example

Function with arguments

Calling a Function:

You can call Javascript functions by simply call the function name.

When you run the above code, it will alert "Good Morning".

Function Arguments (Function parameters) Function Arguments

In javascript you can pass pass any number of arguments while calling a function, no matter how many of them are listed. The parameters you pass to the function will captured inside the function and any manipulation can be done over those arguments. A function can take any number of arguments separated by a comma delimiter

Syntax

Function with one argument

When you run the above code , it will alert "Good morning John".

Function with two arguments

When you run the above code , it will alert 25.

Using arguments as Array

If you don't know the parameters passes to the functions, there is still a way to call array methods on arguments.

Here also you will get the same result because arguments is an array, each argument you pass to the function is added in the "arguments" array.

How to find how many arguments passed in a function ?

Javascript provide an arguments array to store the number of parameters passed in a function, so we can use arguments.length to find out how many parameters passed to a function.

When you run the above code, it will alert "Total number of parameters : 4"

Return Values

Javascript functions have the ability to return a value from a function to the code that called it. Javascript uses the return statement to return a value from a function.

Javascript function can return only a single value. If you want to return more than one values, then you can use an array that contains all of the values as array elements and return it

Javascript Pre-defined Functions

Javascript Pre-defined Functions

Till now , you learned about the user defined functions. But there are many functions that are built into the JavaScript and available for you to use. Some of commonly used built in functions are listed below:

  1. parseInt()
  2. parseFloat()
  3. isNaN()
  4. isFinite()
  5. encodeURI()
  6. decodeURIComponent()
  7. eval()

Sample usage of built in functions

parseInt()

The parseInt() function in Javascript parses a string value and returns an integer value.

When you execute the code, first it will alert "Sum is : 9010" and after using parseInt(), it will alert "Sum is : 100"