Quick Contact

    JavaScript Function

    Introduction

    What is Function?

    A function is a collection of statements that execute specific tasks and may be stored and maintained independently of the main program. Functions enable the creation of reusable code packages that are more portable and easier to debug. Here are some of the benefits of utilizing functions:

    • Functions reduces the repetition of code within a program —

      Functions allow users to simplify frequently used code blocks into a single component. We also can execute the same job in the script by using this function anywhere you want without having to copy and paste the same block of code over and again.

    • Functions makes the code much easier to maintain —

      Because a function generated once may be used many times, any changes made inside a function are automatically applied at all locations without requiring the user to touch several files.

    • Functions makes it easier to eliminate the errors —

      When a code is separated into functions, you know exactly which function is generating the mistake and where to look for it. As a result, rectifying problems becomes significantly simpler.

    Define and Call the Function

    Begin the function declaration with the function keyword, followed by the name of the function you wish to construct, followed by parentheses (), and lastly write your function’s code inside curly brackets{}.

    Syntax

    function functionName() {
        // Code to be executed
    }
    

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Define and Call a Function< /title>
    < /head>
    < body>
    < script>
        // Defining function
    functionsayHello() {
    document.write("Hello, welcome to this DucatTutorial!");
        }
    
        // Calling function
    sayHello(); 
    < /script>
    < /body>
    < /html>
    

    Output

    Hello, welcome to this DucatTutorial!

    Once defined, a function can be called (invoked) from anywhere in the page by typing its name followed by a set of parentheses, such as sayHello() in the preceding example.

    NOTE:

    A function name must begin with a letter or underscore symbol, not a number, and may be proceeded by up to three further letters, numbers, or underscore characters. Function names, like abbreviations, are case sensitive.

    Adding Parameters to Functions

    Whenever user define the function, users may include parameters that will take input values at run time. The parameters function in the same way as placeholder variables do within a function; they are replaced at run time by the values (known as arguments) sent to the function at the time of execution.

    The parameters are set on the first line of the function within the set of parentheses, as seen below:

    function functionName(parameter1, parameter2, parameter3) {
        // Code to be executed
    }
    

    In the following example, the displaySum() method accepts two integers as parameters, adds them together, and displays the result in the browser.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Add Parameters to a Function< /title>
    < /head>
    < body>
    < script>
        // Defining function
    functiondisplaySum(num1, num2) {
    var total = num1 + num2;
    document.write(total);
        }
    
        // Calling function
    displaySum(8, 30); // Prints: 26
    document.write("< br>");
    displaySum(-3, 27); // Prints: 12
    < /script>
    < /body>
    < /html>
    

    Output

    38
    24
    

    We can provide as many parameters as you like. However, a corresponding argument must be supplied to the function when it is called for each parameter you specify, otherwise its value will be undefined.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Pass Arguments to a Function< /title>
    < /head>
    < body>
    < script>
        // Defining function
    functionshowFullname(firstName, lastName) {
    document.write(firstName + " " + lastName);
        }
    
        // Calling function
    showFullname("Ajeet", "Pandey"); 
    document.write("< br>");
    showFullname("Rahul"); 
    < /script>
    < /body>
    < /html>
    

    Output

    Ajeet Pandey
    Rahul undefined
    

    Default Values for Function Parameters

    You may now set default values for function arguments in ES6. This signifies that if the function is called without any arguments, the default parameter values will be utilized. This is one of the most eagerly anticipated JavaScript features.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript ES6 Function with Default Parameter Values< /title>
    < /head>
    < body>
    < script>
    functionsayHello(name='World'){
    return `Hello ${name}!`;
        }
    
    document.write(sayHello()); 
    document.write("< br>");
    document.write(sayHello('Ajeet')); 
    < /script>
    < /body>
    < /html>
    

    Output

    Hello World!
    Hello Ajeet!
    

    Returning Values from a Function

    Using the return statement, a function can return a value to the script that invoked the function. Values of any kind, including arrays and objects, are permitted.

    The return statement is often inserted before the closing curly bracket and is followed by a semicolon.

    Working with Function Expressions

    The syntax we used to define functions previously is known as function declaration. Another syntax for constructing a function is known as a function expression.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Function Expression< /title>
    < /head>
    < body>
    < script>
        // Function Declaration
    functiongetSum(num1, num2) {
    var total = num1 + num2;
    return total;
        }
    document.write(getSum(6, 3) + "< br>"); 
    
        // Function Expression
    vargetSum = function(num1, num2) {
    var total = num1 + num2;
    return total;
        }
    
    document.write(getSum(3, 10)); 
    < /script>
    < /body>
    < /html>
    

    Output

    9
    13
    

    NOTE:

    A semicolon is not required after the closing curly bracket in a function declaration. However, function statements must always terminate with a semicolon.

    The syntax of the function declaration and function expression seems quite similar, however they are evaluated differently.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Function Declaration vs. Function Expression< /title>
    < /head>
    < body>
    < script>
    declaration(); 
    function declaration() {
    document.write("This is function declaration!");
        }
    
    expression(); 
    var expression = function() {
    document.write("This is function expression!");
        }
    < /script>
    < /body>
    < /html>
    

    Output

    This is function declaration!

    FAQS

    Q1. What are JavaScript functions?

    A function in JavaScript is comparable to a procedure—a sequence of instructions that performs a task or calculates a value—but for a process to qualify as a function, it must accept some input and provide an output where the input and output have some clear relationship.

    Q2. How do you call a function?

    You invoke the function by entering its name followed by a value enclosed in parentheses. This value is passed as a parameter to the function. e.g. The function is called firstFunction(“string as displayed.”);

    Q3. What is new function JavaScript?

    Developers can use the launch configuration to build an element of a user-defined entity type or one of the established object types with a function Object() { [native code] } function.

    Q4. What is named function in JavaScript?

    Named Functions: Named functions in JavaScript are just a means of referring to a method that uses the function keyword followed by a name that may be used as a callback to that function. Named functions are regular functions that have a name or identifier.

    Q5. What is array in JavaScript?

    In JavaScript, an array is a single variable that stores several components. It is frequently used when we need to save a list of elements and access them using a single variable.

    Q6. What is node function?

    Node. js functions are utilized directly inside the code. Using Node. js functions allows you to perform the required action of the application in a single process, eliminating the need to start a new thread for each new request.

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.