Computer Science

Javascript Function

A JavaScript function is a block of code that performs a specific task. It is defined using the "function" keyword and can be called multiple times within a program. Functions can take input parameters and return output values, making them reusable and efficient for organizing and executing code.

Written by Perlego with AI-assistance

2 Key excerpts on "Javascript Function"

  • Mastering JavaScript
    Functions are fundamental to JavaScript. Understanding functions in JavaScript is the single most important weapon in your arsenal. The most important fact about functions is that in JavaScript, functions are first-class objects. They are treated like any other JavaScript object. Just like other JavaScript data types, they can be referenced by variables, declared with literals, and even passed as function parameters.
    As with any other object in JavaScript, functions have the following capabilities:
    • They can be created via literals
    • They can be assigned to variables, array entries, and properties of other objects
    • They can be passed as arguments to functions
    • They can be returned as values from functions
    • They can possess properties that can be dynamically created and assigned
    We will talk about each of these unique abilities of a Javascript Function in this chapter and the rest of the book.

    A function literal

    One of the most important concepts in JavaScript is that the functions are the primary unit of execution. Functions are the pieces where you will wrap all your code, hence they will give your programs a structure.
    Javascript Functions are declared using a function literal. Function literals are composed of the following four parts:
    • The function keyword.
    • An optional name that, if specified, must be a valid JavaScript identifier.
    • A list of parameter names enclosed in parentheses. If there are no parameters to the function, you need to provide empty parentheses.
    • The body of the function as a series of JavaScript statements enclosed in braces.

    A function declaration

    The following is a very trivial example to demonstrate all the components of a function declaration: function add(a,b){ return a+b; } c = add(1,2); console.log(c); //prints 3
    The declaration begins with a function
  • JavaScript from Beginner to Professional
    • Laurence Lars Svekis, Maaike van Putten, Codestars By Rob Percival(Authors)
    • 2021(Publication Date)
    • Packt Publishing
      (Publisher)

    6

    Functions

    You have seen quite a lot of JavaScript already, and now you are ready for functions. Soon you will see that you have been using functions already, but now it is time to learn how to start writing your own. Functions are a great building block that will reduce the amount of code you will need in your app. You can call a function whenever you need it, and you can write it as a kind of template with variables. So, depending on how you've written it, you can reuse it in many situations.
    They do require you to think differently about the structure of your code and this can be hard, especially in the beginning. Once you have got the hang of this way of thinking, functions will really help you to write nicely structured, reusable, and low-maintenance code. Let's dive into this new abstraction layer!
    Along the way, we will cover the following topics:
    • Basic functions
    • Function arguments
    • Return
    • Variable scope in functions
    • Recursive functions
    • Nested functions
    • Anonymous functions
    • Function callbacks
      Note: exercise, project and self-check quiz answers can be found in the Appendix .

    Basic functions

    We have been calling functions for a while already. Remember prompt() , console.log() , push() , and sort() for arrays? These are all functions. Functions are a group of statements, variable declarations, loops, and so on that are bundled together. Calling a function means an entire group of statements will get executed.
    First, we are going to have a look at how we can invoke functions, and then we will see how to write functions of our own.

    Invoking functions

    We can recognize functions by the parentheses at the end. We can invoke functions like this:
    nameOfTheFunction(); functionThatTakesInput("the input" , 5 , true );
    This is invoking a function called nameOfTheFunction with no arguments, and a function called functionThatTakesInput
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.