# Prefer anonymous function expression (`ts-compat/prefer-anonymous-function-expression`) 💼 This rule is enabled in the ✅ `recommended` config. 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). The name of a function expression is bound only inside the function itself, so `var f = function foo() { }` gives two names for one function and the second one is useless. Reading it back through `Function.prototype.name` is banned by [no-function-name](no-function-name.md), and IE 8 and below do not implement the binding correctly: the name leaks into the enclosing scope instead of staying inside the function. This rule bans the name and the fix removes it. `Function.prototype.name` needs Chrome 15, Edge 14, Safari 6, Opera 10.5, iOS Safari 6 or Android 18 to run, Firefox has had it since version 1, and no version of IE ever had it. When the body really uses the name, which is how a function expression recurses without a separate variable, the fix leaves that reference behind as an error and it has to be handled by hand. ## Rule Details Examples of **incorrect** code for this rule: ```ts var f = function foo() { }; var g = function bar(n: number): number { return n; }; [1].forEach(function each(el) { }); const cb: () => void = function callback() { }; var o = { f: function inner() { } }; ``` Examples of **correct** code for this rule: ```ts var f = function () { }; var g = function (n: number): number { return n; }; [1].forEach(function (el) { }); const cb: () => void = function () { }; var o = { f: function () { } }; ``` ## When Not To Use It * The name of the function expression is used inside its body. * Stack traces and debugging tools are read from the function names. ## Further Reading * [MDN Function expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function) * [ts-compat/no-function-name](no-function-name.md)