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

Question about spaces between letters/characters

In some parts of the code there is a space on either side of =

ex: var = friendNumber = 1

but later, there are no space

ex: for(var i=0; i < 4; i+=1)

Is there a reason for this? Is it significant?

Thanks

2 Answers

Hi Nikki,

Whitespace is just for us humans so we can read the code easier. Both of these var declarations are valid:

var a = 1;
var a=1;

However keep in mind that sometimes white space is required. This would not be valid:

vara=1;

You need a space between a javascript keyword and its variable name. It works the same in loops:

for(var i = 0; i < 4; i++)
for(var i=0; i<4; i++)

So the short answer: its all about preference. Whitespace makes the codes more readable and its not always required.

Yes there is. When you declare a variable outside a loop, you need to use the space. But inside a statement like for, while, if conditions and others, you don't need to use space, but is much better use it to readability.