Expression closures are a shorthand function syntax for writing simple functions.
function [name]([param1[, param2[, ..., paramN]]]) expression
name
paramN
expression
This addition is nothing more than a shorthand for writing simple functions, giving the language something similar to a typical Lambda notation.
JavaScript 1.7 and older:
function(x) { return x * x; }
JavaScript 1.8:
function(x) x * x
This syntax allows you to leave off the braces and 'return' statement - making them implicit. There is no added benefit to writing code in this manner, other than having it be syntactically shorter.
A shorthand for binding event listeners:
document.addEventListener('click', function() false, true);
Using this notation with some of the array functions from JavaScript 1.6:
elems.some(function(elem) elem.type == 'text');
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | No | No | Yes | No | No | No |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | No | No | No | Yes | No | No | No |
Functions and function scope
Function
function statement
function expression
function* statement
function* expression
GeneratorFunction
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Expression_closures