Câu hỏi phỏng vấn Javascript
Câu hỏi

What does use strict do?

Câu trả lời

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:

  1. 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].

  2. 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].

  3. 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].

  4. 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].

  5. 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].

  6. Prohibits some syntax likely to be defined in future ECMAScript versions: Using certain keywords ...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What are the differences between ES6 class and ES5 function constructors?

middle

Explain the difference between Object.freeze() vs const

junior

Explain what is Linear (Sequential) Search and when may we use one?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào