4

I am currently learning Javascript and came across the term "strict mode". I know that it is used to indicate to the compiler that the code should be examined stricter. Then I went on testing this: first I tried this:

function test() { "use strict"; a = 4; alert(a); }

I didn't get an alert and thought that it is quite logical as an error is thrown because "a" is not defined. Then I tried this:

{ "use strict"; a = 4; alert(a); }

After refreshing the page an alert was shown saying "4". So my question is when is the strict mode "working" and when it isn't?

1 Answer 1

6

Quoting the MDN page on strict mode:

Strict mode applies to entire scripts or to individual functions. It doesn't apply to block statements enclosed in {} braces; attempting to apply it to such contexts does nothing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for quick response. I'll take a note.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.