Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Working With Numbers The Random Challenge Solution

Hey! I Discovered something....

we can even create variables without using var....i just started creating the variables as i do in Python without using any command by mistake and when i looked through my JavaScript file again i discovered that i forgot to use var.... but i was surprised to see that it actually worked without using the var command...lol

my workspace: https://teamtreehouse.com/workspaces/41238132

2 Answers

Steven Parker
Steven Parker
231,269 Points

When you leave off the explicit declaration, the variable is created using implicit global declaration. While this is allowed in normal mode, when the system is in "strict" mode it will cause a fatal error.

Also, even in normal mode, it is considered a programming "best practice" to always explicitly declare variables. There are also some functional differences in behavior between declared and undeclared variables. For more details, see the MDN documentation page for var.

Hey waitπŸ˜… what is strict mode now? And explicit?

Steven Parker
Steven Parker
231,269 Points

To invoke strict mode, put the statement :point_right: "use strict"; :point_left: (including the quotes) before any other statements. For more details, see the MDN page on Strict mode.

"Explicit" declaration is using a keyword ("var", "let", or "const") to create new variables instead of letting the system assume they are globals.