Learn JavaScript Lesson 3 : Comments , Variables & Logic

In this article we start to get to grips with the basic commands that we believe you will use the most in your templates. We will cover , comments , declaration of variables and some simple logic.

All good programmers comment their code. The idea behind adding comments is to explain what the script is doing. The main reason we do this so that if we can no longer look after the code for any reason the person taking over has some understanding of what you have done.

In JavaScript we can have two types of comments, they are

Single Line Comments “//”: This code show you the use of a single line comment


Single line comments can also be used to comment out some code in our script for example :

Multiple Line Comments : This style of comments start with a /* and ends withs a */. The following code shows the use of some multiple line comments.

Now that we know how to comment our code we need to move on and start to write some actual code. Normally when we write script we will need to store the information we have collected in a variable. for example we might want to store the answer to a simple calculation

var iAnswer = 10 + 9;

At this stage we should point out that

•Variable names are case sensitive
•Variable names must begin with a letter or the underscore character

In the above example we stored the value 19 in the variable iAnswer, it is also possible to declare an empty variable for example

var iCount;

If our variable has not been set to anything then we will need to assign it a value in our script. If we don’t do this , then it really means we will not be using it , so we should not have declared it in the first place. Only create variables that you know you are going to use. To assign a value we would use the following

sName="Data Collection";

Now we have our variables we may want to do some calculations with them. When it comes to working with numbers we can use the following

iValue= iAnswer + 5;

and when it comes to working with strings we will need to do something like,

sName = sFirstname + " " + sLastname;
sMessage = "Dear " + sName + ", ";

The last thing we are going to talk about in this article is logic or Conditional Statements. In our templates for our surveys we will need to say things like , “If the value of Q1 = 100 then QAnswer = 100”. to write this in Javascript we would use the following syntax.

if (condition)
  code to be executed if condition is true

The actual code would look something like .

If we wanted to do something like this “If the value of Q1 = 100 then QAnswer = 100 otherwise QAnswer = 0” we could use the If .. else statement. The syntax for this statement is.

if (condition)
  code to be executed if condition is true
  code to be executed if condition is not true

the actual code would look something like.

There are more logic statements that we could talk about , but we think we have covered everything that you will need to know and help you understand the next couple of articles. We have only really touched the surface of what Javascript can do for you so if you would like to learn more than this is a great site to get you started.

6 thoughts on “Learn JavaScript Lesson 3 : Comments , Variables & Logic”

  1. I had some fun with javascript today. This little bit of script causes the form to be submitted when an item is chosen from a dropdown. Basically you can select from the dropdown and go next. It is a bit tricky to get the name of your dropdown. You can get this from the CacheQuestion that is generated when testing in mrStudio or better yet, check out the DDL topic DDL.chm::/interviewscripting_templates_custom_writing.htm to see how to use getElementById.

    • Hi Karen,

      We have just done a quick search on the widgets availabele to print from the blog, there is quite a few so we will install and test a few to see how they perform.

  2. Hi,

    I was not able to get a relational operator to work in javascript. I was trying something really simple like
    var i;
    if (i < 5) {


    The HTML wont work on interviewer when I use operators. It works beautifully for == and !=. Not sure if interviewer misinterprets the < operator to a tag. Any help would be great.


Leave a Comment

%d bloggers like this: