Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Obsolete since Gecko 60 (Firefox 60 / Thunderbird 60 / SeaMonkey 2.57)
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
The expression closure syntax is a deprecated Firefox-specific feature and has been removed starting with Firefox 60. For future-facing usages, consider using arrow functions.
Expression closures are a shorthand function syntax for writing simple functions.
Syntax
function [name]([param1[, param2[, ..., paramN]]]) expression
Parameters
name
- The function name. Can be omitted, in which case the function is anonymous. The name is only local to the function body.
paramN
- The name of an argument to be passed to the function. A function can have up to 255 arguments.
expression
- The expression which comprise the body of the function.
Description
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.
Examples
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');
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Expression closures | Chrome No support No | Edge No support No | Firefox No support 3 — 60 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support 4 — 60 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
Legend
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.