© Russ Ferguson and Keith Cirkel 2017

Russ Ferguson and Keith Cirkel, JavaScript Recipes, 10.1007/978-1-4302-6107-0_12

12. Working with Functions

Russ Ferguson and Keith Cirkel2

(1)Ocean, New Jersey, USA

(2)London, UK

How Do You Create a Function?

Problem

You want to know how many ways there are to create a function.

Solution

There are three ways to create a function, using a constructor, declaration, or expression.

The Code

Listing 12-1. Creating a Function
//function constructor                                                                  var fun1 = new Function('name', 'return name;');fun1('Jessica');//function declaration                                                                  function myFun(name){    var greeting = 'Hello ' + name;    return greeting;}myFun('Danny');//function expression                                                                  var fun3 = function(name) {    return name;}fun3('Mami');

How It Works

There are ...

Get JavaScript Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.