How can JavaScript be abused

Better learn JavaScript via "strict mode"

Originally, the JavaScript programming language was very fault-tolerant due to the history of its origins. The browser developer camps also originally misused JavaScript as a means of power - so JavaScript had to become more fault tolerant in its early days.

This quickly led to error-prone, sloppy programming. And here it is simply helpful to learn the language without error-prone procedures and to switch on the stricter mode ("stict mode"), which quickly indicates errors and problems.

Activate "strict mode"

We can activate for the entire program or only for certain parts of the program. Of course, when learning JavaScript it makes sense to always activate the stricter mode in order to be informed of all errors and possible sources of error and problems.

To do this, "strict mode" is switched on right at the beginning of our JavaScript program:

The quotation marks around ours are important for older browsers that are not yet familiar with this instruction. For these, the line is then just a string and is ignored. It does not matter whether single or double quotation marks are used.

"Strict mode" effects

From now on we are no longer allowed to simply use sloppy variables, but have to declare them (i.e. introduce them with or).

The following JavaScript code would simply be executed without it, even if it is sloppily programmed due to the missing declaration. As soon as our strict mode is switched on, we get an empty browser page (program is canceled) and an error message in the console.

The corresponding error message in the console, which you should always display when developing JavaScript programs and while learning:

The error message can differ depending on the browser, so please do not be surprised if a different error message appears in the course (here Google Chrome is used) than with you when you use a different browser.

The following code would be correct:

There are other effects of the "strict mode" - these will be explained in the course of the course at the appropriate point. So please always use it in the following parts of the course.