Syntax and Spacing – Javascript, HTML, CSS, and beyond

I know a few of my rookie javascript friends/family could use a friendly reminder about syntax. Syntax is important for each language you learn so make sure you review the basics.

Javascript Syntax Basics:

  1. Each line that should be run needs a semicolon at the end
  2. Functions don’t need semicolons after the closing curly bracket
  3. Every time you open a curly bracket, indent your code one tab (or 2 spaces) for readability
  4. When you are writing a callback, finish writing out the method, parens, and semicolon before writing your callback with the parens. This will prevent you from forgetting to close your parens or missing the semicolon at the end. For instance:; // write out the variable and method through the semicolon{return value;}); // then you can write your callback
  5. When writing if, if/else, loops, switches, etc, try to write out the conditions before adding code within the cases For instance:
    // start by writing your if conditions
    if (someNumber > 10) {}
    // then add your code within the curly brackets
    if (someNumber > 10) { return true;}

HTML Syntax Basics:

  1. You need a <html> at the top of the doc
  2. Your file needs a <head></head><body></body> with the html
  3. <head> should only have your metadata, css links
  4. <body> will have the content of the page you are displaying
  5. place your javascript scripts and links at the end of the body
  6. Whenever you open a tag that will be more than one line long, indent with a tab
  7. Line up closing tags with their opening tags

CSS Syntax Basics

  1. you can apply the same styles to a selection of tags/classes/objects by listing them with comma separation before the style
  2. child inherit from their parents
  3. don’t use !important unless you have to


This is just a start. As more tips come to mind, I will add to this list. If you have any notes to add, please send me a comment.