JavaScript Coding Conventions

Modern JavaScript Lets Get Started Writing Code
8 minutes
Share the link to this page
Copied
  Completed

Transcript

In this movie, we're going to take a few minutes to talk about some coding conventions that apply to JavaScript. First off, I want to talk a little bit about case sensitivity. JavaScript is a case sensitive language. What that means is that as an example, here in the app.js file, we set a variable name equal to Steven. Well, if I then wanted to display what was in that variable, and I had typed this instead, it would not find the variable because it's of a different case. Also, if I did console dot log with an uppercase C, that would not work either.

So it's case sensitive. Another thing to mention another convention that I'd like to address is semi colons. I mentioned in previous movie about putting a semicolon at the end of each of these JavaScript statements. Well, technically, that's not required, or you don't have to have a semicolon at the end. However, I strongly recommend that you do that to be a modern JavaScript developer, it's a good idea to always include a semicolon at the end, the semicolon communicates to the JavaScript engine that that statement is ended, and that another statement can begin. There are situations where you can run into problems for example, when you combine JavaScript files, where you can run into problems if you don't have semi colons.

At the end, it will cause those statements to run together and then you will have issues. Also, for readability reasons, it's just a good idea to use semi colon so make that a habit. Make it a habit to always conclude a statement with us. am I calling another coding convention that is important is the use of whitespace. Notice there's a couple places where I've used whitespace in in these two lines that I wrote one as I kept a blank line between the two statements. The reason I did that is because they were had two separate functions.

It's not necessary but it can help with readability. Also within a statement I used whitespace. Around this assignment operator, I had two spaces. That's not required. I could write it this way. However, it just makes it more readable if I use spaces around assignment operators.

Also down here in the console dot log statement. You'll see that I have spaces Hi around the concatenation operator. Some other places where spaces may be important, we haven't learned functions yet is a function declaration. function is just a way to compartmentalize code so that you can call it later it won't execute until you actually call the function. Well notice the spacing, I put but before the curly brace, that's not required but it also helps with readability. There are some places where spacing is necessary.

For example, I could not do that. But other places where it's optional, but we still want to include it. Now something that goes along with whitespace is indentation. indentation also leads to readability. We have yet to cover anything parts of the JavaScript language that were indentation would be used. But let me give you one example.

I had a function statement before. Let me put that up here again. Notice the indentation on the next line. If this is where we started, the code that's in the in the function. We want that line indented and all subsequent lines in debt indented, that improves readability. The same is true for if statements.

Here's a sample if statement checking to see if name. The name variable equals some value. Notice the whitespace I'm using for the curly braces but also there's indentation Hear that we should use. So indentation is important part of making the code more readable. Now suggestion about when you when you create variables is where you're putting more than one word together. For example, if I were to create a variable, first name, equals this is the variable I could have used up above instead of just name.

It consists of two words. Notice what I did with the second word, I capitalize the first letter of the second word, this is called camel case. This is probably the most most common method of, of writing two words together in a single variable name. There are other techniques that can be used. For example, that with a hyphen, or an underscore, but I think the most common mess method is camel case. So maybe something that you want to consider Finally, one last coding convention to get to get you started in this habit early is commenting your code.

A comment in JavaScript begins with two slashes. Any text following those slashes is not executed. As soon as you press return another carriage return, then you can start entering JavaScript again. So it's basically for one line the two slashes is for a single line comment. Now the purpose of comments is once again to make your code more readable. If you remember in the introductory section, we talked about some of some of the items that are important for good programmers one is readability and comments can help with that.

Basically, you want to comment if there's a section of your code that could be confusing to you later or to anyone else that may be working with your code. You should comment it. This code is so simple probably doesn't need a comment. But this gives you an example of how you would use them. So a couple of comments are now what if you wanted to use a do a multi line comment and not a single line comment? Well, that is done this way.

So anything between the slash and the asterisk, and then the asterisk and a slash are considered a comment. As you can see, my editor sublime, Gray's it out as a comment. So that would be for multi line comments. Now one other application of a comment is when you're coding in JavaScript, and you're trying different things trying to figure something out, you may want to get rid of a line, or several statements for that matter. And try your code without those statements to see what happens. A common way to do that is to just comment out that code.

That way, if you still need it, you can come back and remove the comments and it's still there. You don't have to write it all over again. Those are some coding conventions to get started. These are things to remember as you continue to learn additional JavaScript

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.