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

Sebastian Angelo-Perez
Sebastian Angelo-Perez
1,835 Points

Missing "use strict" statement? (According to Dreamweaver)

I just wrote the following code (same as the example from David). When I wrote it in Dreamweaver I got the error: Missing "Use strict" statament...why?

function alertRandom() { var randomNumber = Math.floor(Math.random() * 6 ) + 1; alert(randomNumber); }

alertRandom();
alertRandom();
alertRandom();
alertRandom();

3 Answers

Sebastian Angelo-Perez
Sebastian Angelo-Perez
1,835 Points

Thanks to you both :-) Thanks for the links. I see that one can invoke the strict mode with a simple line of code...apparently this is by default in Dreamweaver when you open a JavaScript file (I seriously need to research more about this, I am still a beginner). After reading your comments I've started to believe that there might not be agreement in the JavaScript community, about using or not using "use strict"? Guys, you are awesome, thanks again. Cheers

Glad the stuff was helpful! But I just wanted to clear up that I agree with Sara that "use strict" is the better practice. Once you do some more research on it you should totally start including it in your projects.

It seems that Dreamweaver wants you to opt into using a strict version of JavaScript. Check out that link to learn about what is means to write you JS in strict mode.

Also, I am not so familiar with Dreamweaver, but there is probably a setting somewhere to turn off the prompt or error messaging for "use strict", since it is not required to write functional JS.

Sara Hardy
Sara Hardy
8,650 Points

While this is a good answer, I'd probably opt to just use strict mode rather than trying to turn off the setting.

Yes, there are advantages to writing JS with "use strict" and virtually no downside now that is has ~90% browser support.

You will find in my previous link to MDN that these advantages include potential performance gains and more secure, "future proof" code. This is because the allowed syntax is adjusted to reflect eventual changes to JavaScript in the browser. Again, you should read the page to gain a better understanding :D

Sebastian Angelo-Perez
Sebastian Angelo-Perez
1,835 Points

ok, I'll do it. There is actually a course here in Treehouse about "use strict"...