1

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.

1
  • you mean let x = 3.14 :D Commented May 30, 2016 at 0:19

3 Answers 3

3

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

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

3 Comments

> Local variables are never a good idea. - you meant "global"?
@GrigoryLoskutov Pretty sure they meant "global"
"… declaring a variable without the var keyword…" is a non sequitur. The only way to declare a variable is with var (ignoring let and const). So if var isn't used, it's not declared. However, variables (well, properties of the global object that behave like global variables) can be created by assignment.
3

You must define the variable x before assigning a value!

"use strict"; let x = 3.14; 

In ES5 you use var for variable declarations. In ES2015 also let or const. Please notice, that ES2015 modules are implicitly in strict mode. Since code is usually structured in modules, you should always work in this mode.

Comments

0

to invoke it you must start by putting:

"use strict"; 

or

'use strict'; 

This will let whatever is running the code to know to use a strict mode instead of a freer mode, which is basically the normal JavaScript where you can do whatever you want (which sounds good on the surface until you have an error somewhere due to a random mismatch and spend hours trying to find it).

I would recommend using it a lot for most things, as it will let you know if something you did was kind of stupid, you forgot to declare something, or something like that, issues that might not have been caught for a while will be caught very quickly this way. For example:

Forgetting to declare a variable, if you just put i = 0; instead of let i = 0;, normal JavaScript might not let you know the error (depending on where it's run), but if you use "use strict";, it'll basically yell at you to fix that. If you didn't declare a variable properly it might lead to the variable being global (I believe), which can cause a lot of issues.

Starting a number with a 0 isn't allowed. Usually, if you start a number with a zero JavaScript acts weird (I think it assumes the number to be octal), which is a problem, so you're not accidentally going to declare something like this and and get away with it (AKA: get a bunch of errors):

let i = 019; 

Also you can't accidentally name a variable a reserved word. I often write my own libraries to use with my code so I sometimes end up using actual reserved words (sometimes leading to everything breaking). If using "use strict";, it'll tell me that the variable name I just chose for the variable is a reserved word, while in normal JavaScript I don't think it lets you know that, which will be a large issue.

There's a lot more about using strict mode in JavaScript, you can see more information online by reading the documentation.

Comments