Related post

JavaScript function : Define and call a function - Zhullyblog


What is a function?




A function is a group of code that can be called later. A function is program designed to perform a task and can be later called anywhere in the code. Let's take for instance, you wrote a code to perform a task and you need the same code later, all you need is to call the function of the initial code instead of rewriting the same code over and over.


Function reduce stress of writing code over and over but instead makes it possible to access the code within it by just calling it.So to work with a function in your code , you need to:

   - Define the function
   - Call the function

Function Definition


I said earlier that a function can be called anywhere in the code but before you call a function, you need to define the function.
To define a function, you use the 'function' keyword followed by the function name.

SYNTAX




function functionName( ) {
         //Statement;
    }
 
Example




//Define a function
function welMessage( ){
  alert("Welcome to Zhullyblog");
}


Screenshot


How to define a function in JavaScript


Now that you have learnt how to define a function, let's move to how to call a function.

Calling a function


To call a function , you call the name of the function. Let's take a look at an example





//Define a function
function welMessage(){
  alert("Welcome to Zhullyblog");
}
//Call a function
welMessage();


Screenshot
How to call a function in JavaScript





Please share













JavaScript Syntax

Where do you place JavaScript in HTML document

JavaScript Variables

JavaScript Strings: How to create and manipulate

JavaScript If, If else, If else if statement


How to generate output in JS

JavaScript Switch case


Comments