Javascript Functions — Understanding The Basics

Explore Functions in JavaScript — declaration, expressions, invocation, and more.


What is a Function?

  • A function is a subprogram designed to perform a particular task.
  • Functions are executed when they are called. This is known as invoking a function.
  • Values can be passed into functions and used within the function.
  • Functions always return a value. In JavaScript, if no return value is specified, the function will returnundefined.
  • Functions are objects.

Define a Function.

There are a few different ways to define a function in JavaScript:
Function Declaration defines a named function. To create a function declaration you use the function keyword followed by the name of the function. When using function declarations, the function definition is hoisted, thus allowing the function to be used before it is defined.
function name(parameters)
{ 
 statements
}
A javascript function does not works untill the function is being called. We need to call the function of the following statements:


functionname(); 



 We can wake up the function by by simply calling the function in the HTML or can be used in input tags a JavaScript function can be return in the head section or in the the body section using the tag.

Example:

<html>
<head>

function ShowMessage()
{ alert("Hello World!");

}

</head>

<body>

<input type=button onclick=ShowMessage()>

 

</body>

</html>



Comments

Popular posts from this blog

Node.js Cheat Sheet

Codeigniter ! Simple But Powerful

Enhancing Supply Chain Security and Cloud-Native Development with Chainguard