Kenneth Choi Kenneth Choi - 6 months ago 16
Javascript Question

Need clarification on what is going on with the "use strict"; syntax on javascript

I have just started to use javascript and recently I have encountered a problem. In this w3schools section in regards to the 'use strict' syntax it states that since x is not defined it causes an error, but from what I see x is equal to 3.14. I am so confused.

"use strict";
x = 3.14; // This will cause an error (x is not defined)


I am in need of clarification to what is actually going on here.

Answer

In JavaScript, declaring a variable without the var keyword makes it a global variable. While JavaScript allows this, it is not a good practice. Global variables are never a good idea.

Part of the appeal of use strict is that it forces good practices. With that enabled, you are required to use the var keyword, otherwise an exception will be thrown.

Here's Mozilla's Documentation on strict mode

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

Comments