Next page | Contents page |

Data types, variables, comments

There are only a few rather general predefined data types in Javascript:

Details of each of those will be given in subsequent pages.


Locations in memory for storing data values. They are given names (identifiers, that the programmer makes up) so that we can keep referring to them. The value assigned to a variable may not be intended to vary (a constant in fact) but that doesn't alter the fact that we put it in a place called a variable.

It is good practice (but not required in JavaScript) to declare variables with keyword var before they are first used, to give them names, allocate memory for them and (optionally) give them initial values:

 var age, nCustomers;    // value: undefined (keyword)
 var paid = false;
 var latitude = 55.0, longitude = -0.5;
 var CM_PER_INCH = 2.54; // A constant

Notice that in the first of these examples age and nCustomers are declared but no values are assigned to them. After this, until something is assigned to them, they have the value undefined (a keyword). You can test (see later) whether a variable has that special value.

So after those 4 statements the system would have allocated some bytes somewhere in memory, labelled so they can be recalled again and containing values like this:


There is no way to say that a particular named variable can only hold data of a particular type. A variable can be reused for anything really but that is not recommended. In other words, JavaScript does not use strong typing as, say, Java does. That makes for flexibility but with increased risk of making mistakes. Try to be aware of the types of data you are using in everything you write. You can of course make the data type a part of each variable name if you feel that would help you (not many do that though; it was a benefit of the Hungarian notation that C/C++ programmers in particular used to use, and we have advised against that! - you decide, but be consistent).


There are two kinds (the same as in C++ or Java):

DO NOT use comments merely to repeat what the code is obviously doing. They could become really confusing if the code is changed.

DO use them to explain why you have done something the way you have, if it is at all unusual. You may need such comments if you need to modify the code a long time later.

It is not difficult for web users to see the JavaScript code and the comments embedded in it. So do not put anything in the comments that should not be seen publicly. It is possible to write programs to strip out comments as a step in publishing to the web but that is beyond the scope of this course.

Note added 2019

The JavaScript specification continues to evolve. Several new keywords have come into use since I started writing this course. Examples: let, const, class. While these can be useful they add subtleties which are likely to confuse beginners. My advice is to learn the language as it was a few years ago and only then look into new features. Another aspect is that new features are not necessarily recognised by all browsers.

Having said that, I have now included a page about let and const but the differences cannot be explained properly until we have covered more material. So the page occurs later in the course: var, let & const.

Next page | Contents page |