Beginner JavaScript – Chapter 2 – JavaScript Statements

Beginner JavaScript – Chapter 2 – JavaScript Statements

Note:

As stated in my initial post, this is part of a series of JavaScript-related posts. As I am becoming knowledgeable in JavaScript, I am posting out what I’ve learned, to re-enforce my knowledge, as well as maybe helpful other web designers/developers in learning JavaScript. If I have mis-stated anything, please feel free to post it in the comments. You’ll help me, and others in learning JavaScript. Thanks!

JavaScript statements are the core building block for anything JavaScript. A JavaScript statement is merely an instruction, telling your JavaScript what to do. Here are some examples of basic JavaScript statements:

var name = "Dennis Deacon";
prompt('What is your name?');
var myDate = new Date();

JavaScript statements follow rules or syntax. Don’t follow the rules, and your JavaScript won’t work.

  • JavaScript is case-sensitive. This means javascript, JavaScript and Javascript are all different. This rules alone will likely cause 80% of your errors in the beginning of using JavaScript.
  • JavaScript statements end with a semicolon (;). While some of code may work without it, missing a semi-colon in the right situations will cause errors. Always best to use at the end of all statements.
  • JavaScript ignores whitespace in statements. Make your JavaScript readable, scannable while developing.

JavaScript allows for commenting in your code. Commenting is a best practice that you should always provide, not only for yourself, but for others that might have to maintain your code in the future.

A single line comment starts with two forward slashes before the commented code. Example:

// This code is commented

If you wish to comment multiple lines of code, you can use the multi-line comment; a forward slash followed by an asterisk, then the reverse (asterisk followed by a forward-slash) at the end of the commented code. Example:

/* This code is commented.
        Make Sure to change the variable below. */

Lastly, when and how to break JavaScript statements.

  • After an arithmetic or related operator (+, , *, /, etc.)
  • After an opening brace ({), bracket ([), or parenthesis (()
  • After a closing brace (})
  • Do not break JavaScript statements after a closing bracket (]) or parenthesis ())
  • Do not break JavaScript statements after an identifier, a value or the return keyword (more on these later)

Up next: JavaScript identifiers.