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
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.
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.
Here are the rules that you should follow when naming a variable.
- 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.
- 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.
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)
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.
'a' 'bat' "cat" "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.
Apostrophe or single quote
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.