Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,149 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
216,149 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.