WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

JavaScript · Advanced · question 54 of 100

What is the concept of "strict mode" in JavaScript, and why is it important?

📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

"Strict mode" is a feature in JavaScript that enforces a stricter set of rules for code execution. When "strict mode" is enabled, certain actions that would otherwise be silently ignored or produce errors are instead flagged as errors, which can help prevent bugs and improve code quality.

To enable strict mode in JavaScript, simply add the string "use strict" at the beginning of a script or a function. For example:

    "use strict";
    function myFunction() {
        // strict mode code here
    }

Here are some of the rules enforced by strict mode:

Variables must be declared before they are used: In non-strict mode, it is possible to create a variable simply by assigning a value to it. In strict mode, variables must be declared using "var," "let," or "const" before they can be used.

Assignment to non-writable properties and undeclared variables is an error: In non-strict mode, assigning a value to a non-writable property or an undeclared variable would silently fail. In strict mode, these actions will throw an error.

Function parameters must be unique: In non-strict mode, it is possible to define multiple parameters with the same name in a function. In strict mode, this will throw an error.

"this" is undefined in functions not called as methods: In non-strict mode, the "this" keyword in a function not called as a method (i.e., not attached to an object) will refer to the global object. In strict mode, "this" will be undefined.

Deleting undeletable properties is an error: In non-strict mode, attempting to delete a non-configurable property on an object would fail silently. In strict mode, this will throw an error.

Strict mode is important because it can help prevent common JavaScript pitfalls and improve code quality. It can catch errors that might otherwise go unnoticed and encourage better coding practices. It is especially useful when working on large codebases or with other developers, as it can help ensure that everyone is adhering to a consistent set of rules.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview — then scores it.
📞 Practice JavaScript — free 15 min
📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

All 100 JavaScript questions · All topics