O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.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

//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');
Listing 12-1.
Creating a Function

How It Works

There are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required