7. Function in JavaScript

 Function in JavaScript

A function in JavaScript is a block of code that contains a group of instructions to perform a particular task.

Syntax :

Function nameOfFunction(parameters-list)
{
// Lines of code to be executed to perform a specific task.
}



Types of Functions in JavaScript
There are two types of functions in JavaScript like any other programming language such as C, C++, and Java.

1.Predefined functions
2.User-defined functions


1.Predefined Functions in JavaScript

1. alert(): This function displays an alert dialog box on the browser.
2. confirm(): This function displays a confirmation dialog box and asks the user to choose one from two options.
3. prompt(): The prompt() function displays a prompt dialog box on the browser and prompts the user to enter input.
4. write(): The write() function used to write something on the document.
5. Date(): This function used to get the current date and time.
6. select(): The select() function used to select the pointed object.
7. parseInt(numString): This function converts a string into an integer.
8. parseFloat(numString, radix): This function converts a string into floating point number.
9. sqrt(number): This function used to get the square root of any number.
10. pow(number): It used to get the power of an integer.


2.User-defined Functions in JavaScript
Like any other robust programming language, JavaScript also allows to create your own user-defined function. 

Function Definition : to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.


Calling Function :
There are some scenarios where we can call a function in JavaScript program. They are:
a) When an event occurs. That is, when a user clicks a button.
b) When the function called (i.e. invoked) from another JavaScript function.
c) Automatically (self called).

Comments

Popular posts from this blog

1. Introduction of Javascript

2. Datatypes & Variables in Javascript

6. Object and Array in Javascript