What does use strict do?
What does use strict do?
The "use strict"
directive is a way to enable strict mode in JavaScript, which is a more restricted variant of the language. Introduced in ECMAScript 5, strict mode imposes stricter parsing and error handling on your JavaScript code, which can lead to better performance, easier debugging, and reduced likelihood of running into issues due to common coding bloopers.
Here are some of the key behaviors enforced by strict mode:
Prevents accidental globals: Without strict mode, assigning a value to an undeclared variable automatically creates a global variable, which can lead to global namespace pollution. In strict mode, this will throw an error, helping to prevent such mistakes[1][2][3][5][6][7][8][9][10][11].
Eliminates this
coercion: In non-strict mode, a reference to this
inside a function that is not a method defaults to the global object (window
in browsers). In strict mode, this
will be undefined
in such scenarios, which can prevent unintended behavior and make code easier to understand[3][5][6][7][8][9][10][11].
Disallows duplicate property names or parameter values: Strict mode will throw an error if an object literal has duplicate property names or a function has duplicate named parameters, which can help avoid errors in your code[3][5][6][7][8][9][10][11].
Makes eval()
safer: Variables and functions declared inside an eval()
statement are not created in the containing scope in strict mode, which can prevent security vulnerabilities and other bugs associated with eval()
[3][5][6][7][8][9][10][11].
Throws error on invalid usage of delete
: The delete
operator cannot be used on variables, functions, or function parameters in strict mode. Attempting to do so will result in an error[3][5][6][7][8][9][10][11].
Prohibits some syntax likely to be defined in future ECMAScript versions: Using certain keywords ...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào