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 Storing and Tracking Information with Variables Variables Review

Invalid JS code questions

The question is something like; which is invalid code, and one of the answers (fortunately the least right answer) is constweeksPerYear = 52. But is this strictly true? Is this really invalid code? I could have declared constweeksPerYear as a variable earlier, and now changed the value to 52. Surely this is not invalid, just because of the letters const at the beginning of the variable name? Not good, not clever name for a variable, I know, but invalid??

1 Answer

Hello

The quiz is not saying the constweeksPerYear is invalid because of the keywork at the start, this is valid. It is saying it is invalid because there is no space between const and weeksPerYear.

You are right in that you will not get an error (unless your in strict mode) as you can create a variable without a prefix in JS, although not recommended and you definitely should not do it.

Given that every other example uses the variable prefix of const it is correct to assume that the invalid one is the one without the space constweeksPerYear as a typo.

Yes, thanks - as I thought. I suppose it looks like I'm being pedantic; but just wanted to clarify whether the example constweeksPerYear was by definition somehow invalid: but it's not; it's just the least valid answer offered. i.e. it's poor coding, not necessarily invalid coding.