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

  • JavaScript location in HTML FileJavaScript location in HTML File
    there is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.
  • How to Turn on JavaScriptHow to Turn on JavaScript
    javascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...
  • What is the difference between Java and JavaScript?What is the difference between Java and JavaScript?
    although the names of java and javascript seem to be related (javascript seems to be a script in java?), but that's the only similarity. these two languages ​​are not technically related to each other.
  • Void keywords in JavaScriptVoid 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.
  • JavaScript code to generate error charts & graphsJavaScript code to generate error charts & graphs
    the example below illustrates a sample variance chart created with javascript that incorporates a column chart. you will also get the source code for reference and editing as you wish.
  • Introduction to 2D Array - 2-dimensional array in JavaScriptIntroduction to 2D Array - 2-dimensional array in JavaScript
    in the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.
  • Operator in JavaScriptOperator in JavaScript
    we see the following simple expression: 4 + 5 is equal to 9. here 4 and 5 are operands and '+' called operator - operator.
  • How to Disable JavaScriptHow to Disable JavaScript
    this wikihow teaches you how to turn off your web browser's javascript support. javascript is responsible for loading dynamic content in webpages, so turning it off can help websites load faster than usual. most web browsers and their...
  • Browser compatibility in JavaScriptBrowser compatibility in JavaScript
    it is extremely important to understand the differences between different web browsers to handle each one in the best way. therefore, you must know which browser your website is running from so that it can provide a suitable solution.
  • How to Use JavaScript InjectionsHow to Use JavaScript Injections
    javascript injection is a process by which we can insert and use our own javascript code in a page, either by entering the code into the address bar, or by finding an xss vulnerability in a website. note that the changes can only be seen...