{{jsSidebar("Errors")}}
Message
SyntaxError: "use strict" not allowed in function with "x" parameter
Error type
{{jsxref("SyntaxError")}}.
What went wrong?
"use strict"
directive appears at the top of a function that has one of the follow parameters.
- {{jsxref("Functions/Default_parameters", "Default parameters", "", 1)}}
- {{jsxref("Functions/rest_parameters", "Rest parameters", "", 1)}}
- {{jsxref("Operators/Destructuring_assignment", "Destructuring parameters", "", 1)}}
"use strict"
is not allowed at the top of such function.
Examples
Function statement
In this case, the function "sum" has default parameters "a=1" and "b=2".
function sum(a=1, b=2) { // SyntaxError: "use strict" not allowed in function with default parameter "use strict"; return a + b; }
If the function should be in strict mode, and the entire script or enclosing function is also okay to be in strict mode, you can move "use strict"
outside.
"use strict"; function sum(a=1, b=2) { return a + b; }
Function expression
Function expression can use yet another workaround.
var sum = function sum([a, b]) { // SyntaxError: "use strict" not allowed in function with destructuring parameter "use strict"; return a + b; };
This can be converted into following expression.
var sum = (function() { "use strict"; return function sum([a, b]) { return a + b; }; })();
Arrow function
If it's an arrow function and needs to access this
variable, you can use arrow function as the enclosing function.
var callback = (...args) => { // SyntaxError: "use strict" not allowed in function with rest parameter "use strict"; return this.run(args); };
This can be converted into following expression.
var callback = (() => { "use strict"; return (...args) => { return this.run(args); }; })();
See also
- {{jsxref("Strict_mode", "Strict mode", "", 1)}}
- {{jsxref("Statements/function", "function statement", "", 1)}}
- {{jsxref("Operators/function", "function expression", "", 1)}}
- {{jsxref("Functions/Default_parameters", "Default parameters", "", 1)}}
- {{jsxref("Functions/rest_parameters", "Rest parameters", "", 1)}}
- {{jsxref("Operators/Destructuring_assignment", "Destructuring parameters", "", 1)}}