JavaScript Function Without Parameter and Without Return

In the previous post, we discussed about the JavaScript function and its components. The functions have different structures such as parameters or no parameters and some function return values and some do not in JavaScript.

The simplest of function is the one without an parameters and without return. The function compute its statements and then directly output the results.

Syntax

Here is the syntax for a function which does not have parameters and also does not return any value.

function <name>()
{
   statements;
   output statement;
}

function – The keyword function identify the block as a function object.

<name> – is the name of the function given by a user.

() – parenthesis does not contain any argument.

statements; – all JavaScript statements that gets executed by the function.

output statement – it is not necessary that a function that does not return anything must give output, but most of the time you will find a statement that prints the output in these type of functions.

Consider the following example program.

Example #1

//variable declarations
var side = 34;
//function declaration
function area_of_square()
{
 var area = side * side;
 console.log("Area of the Square =" + " " + area);
}
//function call 
area_of_square();

Output #1

Output - function with no parameters and no return
Output – function with no parameters and no return

The above example illustrates two things given below.

(1) The variable side is not given to the parameter, but directly accessed by the function which is possible in JavaScript.

(2) The function does not return any value but prints the output to the browser console.

In the next lesson, we will discuss more about other types of function such as function that return some value and its advantages.

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag