JAVA script doubt

in this particular code, why do i have to write Function (){alert("Hello}; and can you please explain this whole code included within tags

which code? plz mention the project number or name or give a picture of code plz


This program is used to demonstrate the function of setTimeout() function.

When the button (Hello guys button) is clicked, the say_hello() function is called.

In this say_hello() function, we use the setTimeout() function (which is a built in javascript function) we pass two parameters to the setTimeout() function, a function and time interval.

  1. first parameter: we must pass a function as the first parameter. So we create a function here itself which has an alert() method to display a popup box saying “Hello”. (alert() method is used to display a pop-up box/dialogue box which is used for alerting/notifying something )


  1. second parameter: we must specify the interval of time (in milliseconds) after which the function should execute. The output of this function will be displayed after the interval of time specified, rather than immediately.


the complete setTimeout function according to the program will be:

setTimeout(function(){ alert(“Hello”); } , 3000);

Thus the dialogue/pop-up box will appear after 3000 milliseconds i.e. 3 seconds after the button “Hello guys” is clicked

The order of execution:

“Hello guys” button is clicked,
say_hello() function is called which has setTimeout() function inside it,
because of the setTimeout() function the pop-up box/dialogue box having the content “Hello” will appear 3 seconds after the button was clicked.

Hope I helped in clearing your doubt.


function is the keyword in Javascript that is used to create functions.
Statements are enclosed within a function to repeatedly execute it or to reuse the bunch of statements for a particular task.

the syntax / structure of a function in javascript is as follows :
function name_of_the_function()
setTimeout() is a predefined function which usually takes 2 arguments: function name and milliseconds
this function is used to call a function or evaluate an expression after the amount of milliseconds mentioned as arguments.

The above code you asked to explain creates a function named say_hello
The setTimeout() takes arguments :
(i) First a function that displays an alert message “Hello”
(ii) 3000 milliseconds (that is 3 seconds).

Hence when you call the say_hello() function it’ll display an alert message “Hello” exactly after 3 seconds.

To know more about functions, refer to
to know more about the setTimeout() predefined function, refer to

Hope this solved your doubt :slight_smile:

can you tell me what is the name of " function(){ alert(“Hello”); }" this function and how it is working. I am not able to understand it.

setTimeout() and setInterval() functions allow you to execute a piece of JavaScript code/function at a certain point in the future. setInterval repeats the call, setTimeout only runs it once.

JavaScript setTimeout(expression, timeout); runs the code/function once after the timeout. It is a time based code execution method that will execute script only one time when the interval is reached, and not repeat again unless you gear it to loop the script by nesting the setTimeout object inside of the function it calls to run. If geared to loop, it will keep firing at the interval unless you call clearTimeout(). If you want something to happen one time after some seconds Then use setTimeout… because it only executes one time when the interval is reached.

setTimeout(function() {
  console.log('Wait 3 seconds and I appear just once');
}, 3000);

setInterval(expression, timeout); runs the code/function repeatedly, with the length of the timeout between each repeat. It is a time interval based code execution method that has the native ability to repeatedly run specified script when the interval is reached. It should not be nested into its callback function by the script author to make it loop, since it loops by default. It will keep firing at the interval unless you call clearInterval(). If you want to loop code for animations or clocks Then use setInterval.

setInterval(function() {
  console.log('Every 3 seconds I appear on your console');
}, 3000)