Types of Statements in JavaScript – Programming Foundations #06

Let’s talk about the types of statements in JavaScript.
So what is a JavaScript statement? By now you know that a computer program is a series of instructions that are written to be executed by the computer. Each of these instructions is called a JavaScript statement.

Each JavaScript statement in a program holds a part of code. Together, these statements can determine the behavior of the program.

types of statements in javascript

The different types of statements in JavaScript are written using the following components

  • Values
  • Operators
  • Keywords
  • Comments
  • Expressions

To understand this better, take a look at the set of statements given below.

var a = 2;
var b = 2;
var total;
total = a + b;
alert(total);
types of statements in javascript
types of statements in javascript (in Notepad++)

Try this JavaScript code. You can use the same HTML file we created in the previous tutorial. The resulting alert box that pops up when you run the HTML file after entering this JavaScript code shows you the total of variable a and variable b, which is 4.

There are three variables in the above code : a , b and total. We will discuss more about JavaScript variables in another article.

You can see that the different JavaScript statements are separated using semi-colons. A semi-colon must always be added to the end of every executable statement. You can also have different statements in the same line by adding semi-colons.

var a, b, total;
a = 2; b = 2; total = a + b;
alert(total);
types of statements in javascript
types of statements in javascript

The computer will execute your JavaScript program statement by statement, in the precise order that they have been written.

Working with Operators in JavaScript statements

We use operators when writing many different types of statements in JavaScript.

  • =   the equal sign
  • +   addition
  • –   subtraction
  • *   multiplication
  •  /  division

Using Keywords in JavaScript statements

Keywords are special words or symbols that have a very specific meaning in JavaScript programming language. There are many keywords in JavaScript, bu here are a just a few that you will soon be using frequently;

  • var – to declare a variable
  • function – to declare a function
  • if – assigning a set of statements to be executed depending a specific condition
  • return –  to stop working in a function and return a variable
  • while – to assign a set of statements to be repeated while a certain condition holds true.
  • switch –  to assign a set of statements to be executed depending on different situations.

Using Comments in JavaScript

You can add any comments you like to JavaScript statements by placing two slashes beside the line of code. These comments are ignored by JavaScript and the program will run the same. Do try out the code below.

//this is a comment
var name = prompt("What is your name?"); //'prompt' is used to get user input
alert("Hello " + name + "!"); //greet user using their response
types of statements in javascript
types of statements in javascript

Expressions in JavaScript statements

An expression is a piece of code that can be evaluated down into one value. Different types of statements in JavaScript can use varying expressions.

Types of expressions include String expressions, Arithmetic expressions, Logical expressions, Object creation expressions, Function defining expressions and much more. We’ll be looking more at expressions more in a later article.

Take a look at this numeric expression.

var realPrice = price - (price * (discount/100));

And here is a string expression.
var sweet = "Pumpkin" + " Pasty";

White Space in JavaScript

JavaScript, like a lot of modern programming languages, ignores white space in your code. White spaces can be line breaks, tabs or spaces between words. They are considered to be inconsequential to the program. So go ahead and add as many white spaces as you want to make your code look organized and easier to read.

What’s next? Here’s the next article on variables and values in JavaScript and how you can use them. You can also take a look at our other JavaScript tutorials.

While you’re here feel free to check out our other programming tutorials. We have tutorials on Python, Java and other coding tidbits you’ll love!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.