JavaScript Lesson 7: Statements

0
15

JavaScript Statements

Every command or line in JavaScript that you’ve written, for the most part, has been comprised of separate JavaScript statements. Whether it be calling functions or outputting data, your code is compromised of many statements. We discussed statements briefly in our first lesson on Syntax. To reiterate, sentences in JavaScript are called statements. To make it simple, statements are commands on what you want the browser to do. Constructing these statements in a way that the computer understands it and interprets it is the language syntax.

Typical Statement Ending

Typically the end of a JavaScript statement is marked with a semicolon (;), however we have been told that the semicolon is option in JavaScript. In JavaScript, the end of a statement is most often marked by pressing the return key and starting a new line.

Categories of Statements

In addition to standard statements such as changing a variable’s value, assigning a new value, or calling a function, there are distinct groups of statements which each serve a specific purpose. We will provide a brief overview of each of these categories in this lesson and cover them in greater detail later in on our tutorials. These distinct groups of statements include:

  • Conditional Statements
  • Loop Statements
  • Object Manipulation Statements
  • Comment Statements
  • Exception Handling Statements

Let’s explore these categories in a little more detail

Conditional Statements

If you already know what conditional statements then you can skip this paragraph! Else you should continue reading to get an idea. If you didn’t guess already, those first two sentences is an example of conditional statements. Conditional statements usually consist of an if question if( x = 5) followed by what to do if the statement is equal to true. However, if the conditional statement isn’t true, you can define what should be done with your else statement. Let’s put it into perspective.

Conditional statements are used to control your scripts so that different actions can be taken depending on the situation. For example, perhaps you want to display a greeting depending on what time of day it is. Your condition would depend on what time it was (before noon, afternoon, evening) and output the corresponding greeting. This gives you scripts a more dynamic approach and is one of the most powerful tools in programming!

Loop Statements

Ugh, don’t you just hate having to do things over an over again? Repetitive tasks are always a burden to us. Fortunately in JavaScript you have the ability to create loops to avoid tedious and monotonous repetitive tasks with just a little bit of thinking outside the box.

The main idea behind loop statements is to do something over and over again until the task has been completed. This type statement can make your life a heck of a lot easier. We will explore this more later on and trust me, this is something worth looking into!

Object Manipulation Statements

Object Manipulation Statements are statements designed to manipulate the object model in order to accomplish tasks. This is an advance topic which will discuss later on. Don’t worry about it now!

Comment Statements

Don’t underestimate the power of comment statements! Comments are statements that do not effect the coding at all and mainly serve as a way to write notes inside your code. They can be effective for explaining:

  • How the code works
  • Why is was written that way
  • Things to improve

The idea is that when you read your code after not seeing it for a while, you may have forgotten details as to how or why. This way you can leave a note for your future self (or even another developer) to make your future life easier!

Exception Handling Statements

This is an advanced programming subject that is not necessary for most JavaScript programmers. Exception handling statements consist of the try, catch and throw statement which tries to execute a piece of code and if it fails, the catch should handle the error gracefully.

For example, there maybe instances when you are programming that you do not know for sure if the file that you will be writing to, the input stream you are reading from, or the connection you have established will be usable for the code that you want to execute. By using the try, catch and throw statements, you can program safety mechanisms, so that your code handles common problems that may arise (i.e. the input stream you are reading from suddenly disappears).

Summary

  • JavaScript statements define what the JavaScript script will do and how it will be done
  • JavaScript statements don’t necessarily have to end with a semicolon (;).
  • JavaScript Categories include
    • Conditional Statements
    • Loop Statements
    • Object Manipulation Statements
    • Comment Statements
    • Exception Handling Statements

Next Lesson

Don’t fret to much if you have not mastered everything in this chapter as it was mainly to serve as an overview. Now we will explore these statement categories in more detail starting with Conditionals.

Leave a Reply