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 trialKyle Vassella
9,033 PointsTo space or not to space?
I'm on the beginner's Javascript lesson on Variables. In it, he types the Variable code as follows:
var message = "Hello";
Note that he has a space on each side of the equals sign. I was tinkering around and noticed that erasing both of these spaces made no difference in the end result displayed.
I was just wondering which way is more proper. It seems that it might save some (very slight) time not typing the spaces, but I'm guessing the professional way is to include the spaces to make everything look neater (which, in itself may save someone else looking at your code some time).
Which way is more proper in a professional setting? Kind of a dumb question, because I doubt he would show us the 'less proper' way...but wanted to ask just in case.
5 Answers
Joey Ward
Courses Plus Student 24,778 PointsWhen writing JavaScript, and CSS, the interpreter actually ignores spaces entirely. Meaning you don't need any spaces in order for your code to work. Your entire JavaScript file could be one long string of text all on one line with no spaces at all.
That doesn't sound very easy to read for a developer, but the lack of spaces and line breaks actually makes the file smaller, and therefore faster for the browser to retrieve from the server and read.
So which style do you choose to write your code with? Well obviously your code would be extremely hard to follow and update if it was all smushed together as one giant string. Although the choice to use or omit spaces around an equals sign is less drastic than omitting spacing and line breaks from your JS file entirely.
The good news is that there are tools to minify, or "uglify" your code. Check out the classes on GULP to understand how to concatenate and compress large projects into minified files. You can also minify your code right in your browser with sites like http://jscompress.com/ or https://javascript-minifier.com/.
The minification process strips out all spaces, line breaks, and comments. Most popular frameworks deliver a minified file. For example, the Twitter Bootstrap framework (www.getbootstrap.com) shows you both the source file with formatting, comments, and spacing in addition to the minified file. It's easy to see the difference.
So the answer is you can code with whatever spacing and linebreak conventions work best for you. Remember to use commonly accepted formatting conventions if you expect others to contribute to or revise your code in the future. Once you've written your full JS file, save the original file for future editing, but deliver a minified version of the file to your site, assuming the original JS file isn't already tiny.
Thomas Fildes
22,687 PointsHi Kyle,
It's very common for variables to have spaces in between its name and value. It is a neater way to display JavaScript and makes it easier for other developers who have to read the code if they need to update it in the future. Also, I personally like to use spaces in parameters in if statement conditions like this below:
if ( 5 > 1 ) { // This is better than the one below because its easier to read
}
if(5>1) {
}
Kyle Vassella
9,033 PointsThank you both for taking the time to give me those great replies. Funny enough, shortly after reading your replies I realized my next video lesson covered spacing! Hah. When it rains, it pours! Thanks again.
Andrea Sanchez
2,118 PointsHaha, I've thought the same thing and was surprised it was never mentioned in any videos I've seen so far. I think it's a very good question and I bet common question amongst us newbie's.
Randy Eichelberger
1,491 PointsGenerally if you are writing code for a company or for an open source project their "code conventions" will require spacing. Code conventions are just the specific style of writing code applied, with most organizations using certain common conventions such as the way variable names are written, or spacing.
While not required, as has been stated spaces simply help the developer (and any future developers reading the code) to understand what it says easily. When you start getting into larger projects and you're looking through a ton of lines of code for hours a day you want any little thing that can help.
That said it's not necessarily required, and as has been talked about previously there is a thing called "minifying" Here is the wikipedia link for minification that I recommend anyone looking to write code for the web read, as you will see it out in the real world a lot. As you get the vanilla JavaScript down you will begin using libraries such as Angular.JS and Express.JS (If you're doing the fullstack JS track you'll run into this sooner than later). If you look at the files installed when using npm/bower you will notice that bootstrap, Angular, etc generally provide a minified version of the file, usually names something like "angular-min.js"