1// Whole-script strict mode syntax
2'use strict';
3var v = "Hi! I'm a strict mode script!";
4
1/*
2even though x is not defined with a keyword like var, let or const, the
3browser won't throw an error
4*/
5x = 1;
6
7function myFunc() {
8 "use strict";
9
10 /*
11 this will throw an error as y is being assigned a value without it
12 being declared AND "use strict" has been used for the function
13 */
14 y = 4;
15}
16
17/*
18Basically, "use strict" is a way for programmers to avoid bad habits by using
19invalid syntax which browsers accept but is still wrong
20*/
1Strict mode changes previously accepted "bad syntax" into real errors.
2
3As an example, in normal JavaScript, mistyping a variable name creates a
4new global variable. In strict mode, this will throw an error, making it
5impossible to accidentally create a global variable.
6
7In normal JavaScript, a developer will not receive any error feedback assigning
8values to non-writable properties.
9
10In strict mode, any assignment to a non-writable property, a getter-only
11property, a non-existing property, a non-existing variable, or a non-existing
12object, will throw an error.
13
14Strict mode makes it easier to write "secure" JavaScript.
1// Whole-Script Strict Mode Syntax
2'use strict';
3var v = "Hi! I'm a strict mode script!";