04 - Variables and simple types in javascript

Variables, value and more in Javascript

Now that we know where we are going to place our code and get it to work with HTML, we have to take our learning to the next step.

Reserved words or keywords

Consider a spoken language like English. We have sounds which make words. Each word has a meaning and is intended to serve a purpose. Similarly in Javascript like language, there are certain words which are intended to indicate to the parser certain purpose. These words are reserved for a certain purpose and will cause errors if used for any other purpose.

Here is the list of reserved words or otherwise called keywords in Javascript. Each word has a specific functionality which we will learn as we proceed further. For now, it is good to just know them.





Just remember, do not use the above list of words for any purpose other than what they are intended for.

You wish to design code that performs a function. Isn’t that right? May be you want to develop an online supermarket or a calculator. Whatever it may be, we will be handling information. As a human being, whenever we want to remember something, we will memorise it. To simply put it, we are storing the information in our memory.

Sometimes, we feel that our memory can’t take in any more of information. At those times we feel our heads are full (which is not true!). Likewise, the computer also has memory that has a limited capacity beyond which information cannot be stored properly.

Whenever we remember information by something, it is easier to recollect it. Whenever we want to remember things like list of rules or commandments we try to make a tagline of the starting letters or something like the 9 planets.


Whenever we want to store some information in Javascript, we will do so using variables. The information is allocated some space in the memory and a name is given to associate with the information. So whenever we want to refer to that information, we simply use the name. This makes it easy for Javascript to give you the information and also easy for you to refer to it.

The ‘var’ keyword followed by the name creates a variable. The information which is associated with the variable is called the value. Whenever you associate a variable name with the information, you are said to be storing the value in the variable, sort of like a container. The value can be changed as and when you need it.

In case you want to create a calculator application, you would need to store the two operands and the operation. So, here is how you can create them.

     var operand1, operand2, operation;

var operand1;

var operand 2;

var operation;

Notice how friendly these names are.  These are the two forms of declaration. You can declare each variable you want in single lines as in the first snippet or as a multi-line declaration as shown in the second snippet. One more thing that you should remember is that (almost) every line should end with a semicolon.

They can easily help you reach out to the correct information you want. There are some rules that you would be required to follow whenever you name a variable.

Before we proceed to the rules, we should know what white space is.

White space is the space between two words. This space helps demarcate two words and hence help in making the code more readable. So, a white space after a word indicates the end of a word. In the English language, people tend to start new paragraphs with the first line started a few spaces away from the margin. Similarly in Javascript, indentation can help in building levels and makes the code more readable.

Here are the rules that you should follow when naming a variable.

  1. A variable name should begin with a letter or an underscore. No other special symbols are allowed. Numbers can occur in the middle or at the end but not at the beginning.
  2. Case sensitivity is present in Javascript. So, Operand1 and operand1 are different.
  3. Reserved words or keywords are not to be used as variable names. They are indeed reserved.

Before we move onto literals, we will put in a small word about functions. A function as its name implies can perform a particular functionality. Just remember this for now to understand code snippets easily.

There are different types of variables in Javascript. The type of the variable is determined from the type of value stored in the variable. Literals are used to specify values to a variable. Each literal has a fixed value. For example 1 is meant to be the numerical value 1 and does not imply any other value.

Integer literals

As the name implies, these literals can specify integer values. You can specify them in decimal or base 10 format, octal or base 8 format, hexadecimal or base 16 format.

If a leading 0 precedes the literal then it is in octal representation. (0-7)

If a leading 0x or 0X precedes the literal then it is hexadecimal representation. (0-15, A-F)

var plain_integer = 10;    à Integer represented in decimal format

var octal_integer = 010;  à integer represented in octal format

var hex_integer = 0x1;  à integer represented in hexadecimal format


 Floating point literals

One can specify floating point values by these types of literals. They are represented in fractional format where there is the a dot (.) preceding which the integer part comes and the fraction comes after it, and the exponential format in which the letter e or E comes.

Here are some examples of floating point literals.








These are the literals that can help you store letters, phrases, words or even sentences. Each literal is placed within single quotations or double quotations. Mind you, both the opening and the closing quotation marks should be of the same type. Here are some examples of string literals.

"dogs bark"
'Eggs fry'

One should note here that 1 is different from ‘1’ as 1 is an integer while ‘1’ is a string.

Special characters and escape sequences

So, a string is contained within quotes. We use the same symbol for a single quote as well as an apostrophe. We would like that apostrophe to be displayed as such. What should we do now? We use a back slash as an escape character. The backslash and the letter or symbol following it is called as an escape sequence.

var whose_bread = ‘John\’s bread’;

Similarly if we want to display a backslash then what would we do? We use a backslash then too.

var file_path = “c:\\myfile”;

Like said before, certain escape sequences have a predefined meaning.






Form feed


New line


Carriage return




Vertical tab


Apostrophe or single quote


Double quote


Backslash character


The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.



The character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol.



The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol.


Now you may wonder what will happen with other characters. Usually the browsers ignore the back slash but sometimes, the usage may cause errors. It is better to avoid such usages.


Sometimes we have to store logical values true or false. The boolean literals true and false are specified without quotes. If done so, they become strings.

var is_greater_than_zero = true;
var is_negative = false;

The above literals types are simple. When we want to store complex information, we use objects and strings. We will deal with them separately.

Like us on Facebook