Understanding Strict Mode
JavaScript Fundamentals: Strict Mode
When was ECMAScript 5 (ES5) released for use?
View Answer:
Interview Response: ECMAScript 5 (ES5) was released for use in 2009.
How do you explicitly enable ES5 features and modifications?
View Answer:
Interview Response: You need to enable them with a special directive explicitly: "use strict".
Code Example:
"use strict";
// this code works the modern way
...
Explain how you should implement strict mode in JavaScript?
View Answer:
Interview Response: Strict mode is enabled by placing “use strict” at the top of your script.
Code Example:
alert('this is not strict');
// "use strict" below is ignored
// -it must be at the top of the script
('use strict');
// strict mode is not activated
Can you cancel strict mode later in your code?
View Answer:
Interview Response: No, there is no directive like "no use strict" that reverts the engine to the old behavior. Once we enter strict mode, there is no going back.
Does the browser developer console implement strict mode by default?
View Answer:
Interview Response: No, we must place it at the first console line for it to work.
How do you implement strict mode in the browser console?
View Answer:
Interview Response: We must place it at the first console line for it to work, then add the rest of our code.
Technical Response: First, you can try to press Shift+Enter to input multiple lines and put “use-strict” on top. In Older browsers, you have to put it in a wrapper.
Code Example: works in most browsers, namely Firefox and Chrome.
'use strict';
// <Shift+Enter for a newline>
// ...your code
// <Enter to run>
Code Example: In Older browsers, you have to put it in a wrapper.
(function () {
'use strict';
// ...your code here...
})();
Should we implement ‘use strict’ in modern applications?
View Answer:
Interview Response: The implementation of “use strict” remains recommended, in most cases.
Technical Response: Yes, it remains recommended to use strict mode in all modern applications. Although it may not be essential in some cases, we should provide the strict-mode functionality.
Are there any language structures that implement strict mode by default?
View Answer:
Interview Response: Yes, JavaScript classes and modules implement strict mode by default.