JavaScript Function – Tutorialpath

Hello to all of you, thanks for landing on the TutorialPath.com, which is “Your Path to Learn Everything” in an easy way and you can learn almost every sort of important elements about online things. As you land here, so we can assume, you are a newbie for JavaScript Function language, so you don’t need to worry about it.

JavaScript Function Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which helps you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world.

As you know technology is one of the trending parts of this world. So, here again, we came up with the latest and new tech information which is related to the process of creating a JavaScript Function. With the help of this article, you will get to know the complete process of how to create a Basic JavaScript Function in a step by step manner. So, simply have a look at this article and grab all the useful information which is going to be very helpful for you.

JavaScript Function

JavaScript Functions

JavaScript provides functions similar to most scripting and programming languages.

JavaScript Function

In JavaScript, a function allows you to define a block of code, give it a name, and then execute it as many times as you want.

Like any other advanced programming language, JavaScript also supports all the features required to write modular code using functions. You may have seen works like alert () and writing () in earlier chapters. We were using these functions repeatedly, but they were written only once in core JavaScript.

A JavaScript function can be defined using the function keyword.

Syntax

The basic syntax is shown here.

//defining a function
function <function-name>()
{
    // code to be executed
};

//calling a function
<function-name>();

Function Definition

The function keyword goes first, then the function name, then a list of parameters (separated by commas, blank in the example above) between the brackets and finally the code of the function, also known as the “function body”, Curly braces between.

The rules for creating a function in JavaScript:

  • Each function must begin after the keyword function,
  • A user-defined function name that must be unique,
  • A list of parameters enclosed within parentheses and separated by commas,
  • {} A list of statements that compose the anatomy of a function enclosed within a curly brace.

Example: Define and Call a Function

<!DOCTYPE html>
<html>
<body>
<h1>Example: JavaScript function</h1><script>
function ShowMessage() {
alert(“Hello Tutorialpath!”);
}ShowMessage();
</script>
</body>
</html>

Output:

Example: JavaScript function

Calling Function

To conduct a function somewhere later in the script, you will just need to write the name of the function as shown in the following code.

Example:

<html>
<head><script type = “text/javascript”>
function sayHello()
{
document.write (“Hello there!”);
}
</script></head>
<body>
<p>Click the following button to call the function</p><form>
<input type = “button” onclick = “sayHello()” value = “Say Hello”>
</form>

<p>Use different text in write method and then try…</p>
</body>
</html>

Output:

 Click the following button to call the function

 

 Use different text in the write method and then try…

Function Parameters

A function may contain one or more parameters, which will be supplied by the calling code and can be used inside a function. JavaScript is a dynamic type of scripting language, so a function parameter can contain a value of any data type.

Example: Function Parameters

<script type = “text/javascript”>

// Function definition

function welcomeMsg(name) {

document.write(“Hello ” + name + ” welcome to Tutorialpath”);

}

// creating a variable

var nameVal = “Admin”;

// calling the function

welcomeMsg(nameVal);

</script>

Output:

Hello Admin welcome to Tutorialpath

Return Statement:

There are some circumstances when we want to return some value from a function after performing some operation. In such cases, we can use the return statement in JavaScript. This is an optional statement and most of the time the last statement in the JavaScript function.

See our first example with a function named Call Addition. This function is calculating two numbers and then returning the result. The most basic syntax to use the return statement is:

return value;

Conclusion

So, hopefully, we have shared all the basic steps that help you with JavaScript Function.  PHP extract the last character from a string. In this tutorial, you have several ways for PHP to remove the last character from any given strings.

Leave a Reply

Your email address will not be published. Required fields are marked *

//azoaltou.com/afu.php?zoneid=3461279