JavaScript functions

A function is a group of reusable code that can be called anywhere in your program. This makes it unnecessary to write the same code over and over again. It helps programmers write modular code. Functions allow a programmer to divide a large program into small and manageable functions.

Like any other high-level programming language, JavaScript also supports all the features needed to write modular code using functions. You have seen functions like alert () and write () in the previous chapters. We have used these functions many times, but they have only been written in core JavaScript.

JavaScript also allows us to write our own functions. This section explains how to write your own functions in JavaScript.

Function definition

Before we use a function, we need to define it. The most common method for defining a function in JavaScript is by using the Function keyword, followed by a unique function name, a list of parameters (which may be blank), and a block of statements surrounded by the brackets.

Syntax

Simple syntax is as follows:

  type = "text/javascript" >  

For example

Try the following example. It defines a function called sayHello that doesn't take any parameters:

  type = "text/javascript" >  

Call a function

To call a function somewhere after the script, you will simply need to write the name of the function like the following code:

  type = "text/javascript" > function sayHello () { document . write ( "Hello there!" ); } 
 Click the following button to call the function  type = "button" onclick = " sayHello () " value = "Xin chao!" > 
 Use different text in write method and then try. 

Result

Following the above method will produce results

Parameter of function

To this chapter, we saw functions without parameters. But there is a convenience when passing different parameters while calling a function. These passed parameters can be captured inside that function and any operation can be performed on those parameters. A function can take multiple parameters separated by commas.

For example

Try the following example. We edited the sayHello function here. Now it takes two parameters.

  type = "text/javascript" > function sayHello ( name , age ) { document . write ( name + " is " + age + " years old." ); } 
 Click the following button to call the function  type = "button" onclick = " sayHello ( 'Zara' , 7 ) " value = "Xin chao!" > 
 Use different parameters inside the function and then try. 

Result

Following the above function will produce the result

Return command

A JavaScript function can have an arbitrary r eturn command. This command is required if you want to return a value from a function. This command should be the last command of a function.

For example, you can pass two numbers in a function and then you can expect that function to return the result of multiplying those two numbers in the program that calls your function.

For example

Try the following example. It defines a function that takes two parameters and joins them before returning the result in the function call.

  type = "text/javascript" > function concatenate ( first , last ) { var full ; full = first + last ; return full ; } function secondFunction () { var result ; result = concatenate ( 'Zara' , 'Ali' ); document . write ( result ); } 
 Click the following button to call the function  type = "button" onclick = " secondFunction () " value = "Call Function" > 
 Use different parameters inside the function and then try. 

Result

Following the above function will produce the result

According to Tutorialspoint

Previous article: Loop control in JavaScript

Next article: Event (Event) in JavaScript

4.5 ★ | 2 Vote

May be interested

  • Event (Event) in JavaScriptPhoto of Event (Event) in JavaScript
    javascript interaction with hmtl is handled through events that occur when the user or browser manipulates a page.
  • Page navigation (Redirect) in JavaScriptPhoto of Page navigation (Redirect) in JavaScript
    you may have a situation when you click on a url to go to page x but you are directed to page y. it happens because page redirection - redirects the page. this concept is different from: javascript - refresh page.
  • Dialogs - Alert, Prompt, Confirmation in JavaScriptPhoto of Dialogs - Alert, Prompt, Confirmation in JavaScript
    javascript supports 3 important dialog types. these dialogs can be used to notify, confirm input, or receive input from users. below we discuss each type of dialog box.
  • Void keywords in JavaScriptPhoto of Void keywords in JavaScript
    void is an important keyword in javascript that can be used as a unary operator before its single operand, which can be in any type. this operator defines an expression to be evaluated without returning a value.
  • Print pages in JavaScriptPhoto of Print pages in JavaScript
    many times you will love to put a button on your website to print the content of that page through a printer. javascript helps you perform this function by using the print function of the window object.
  • Objects in JavaScriptPhoto of Objects in JavaScript
    javascript is an object oriented programming language (object oriented programming). a program language can be called object-oriented if it provides four basic capabilities to the programmer.