Variables in Java Script Java Script

Variables in ECMAScript are defined by using the var operator(short for variable),followed by the variable name,such as:

In this example, the variable test is declared and given an initialization value of “hi”(a string). Because ECMAScript is loosely typed, the interpreter automatically creates a string value for test without any explicit type declaration.You can also define two or more variables using the same var statement:

The previous code defines the variable test to have a value of “hi” and the variable test2 to have a value of “hola”.Variables using the same var statement don’t have to be of the same type, however, as shown in the following:

This example defines test (yet again) in addition to another variable named age that is set to the value of 25.Even though test and age are two different data types,this is perfectly legal in ECMAScript.

Unlike Java,variables in ECMAScript do not require initialization(they are actually initialized behind the scenes, which I discuss later). Therefore, this line of code is valid:

Also unlike Java,variables can hold different types of values at different times;this is the advantage of loosely typed variables.A variable can be initialized with a string value,for instance, and later on be set to a number value,like this:

This code outputs both the string and the number values without incident (or error). As mentioned previously, it is best coding practice for a variable to always contain a value of the same type throughout its use.

In terms of variables names, a name must follow two simple rules:

  • The first character must be a letter, an underscore (_),or a dollar sign ($).
  • All remaining characters may be underscores, dollar signs,or any alphanumeric characters.

All the following variable names are legal:

Of course, just because variable names are syntactically correct doesn’t mean you should use them.Variables should adhere to one of the well-known naming conventions:

  • Camel Notation — the first letter is lowercase and each appended word begins with an uppercase letter. For example:
  • Pascal Notation —the first letter is uppercase and each appended word begins with an uppercase letter. For example:
  • Hungarian Type Notation — prepends a lowercase letter (or sequence of lowercase letters) to the beginning of a Pascal Notation variable name to indicate the type of the variable. For example, i means integer and s means string in the following line:

Variables in Java Script

Another interesting aspect of ECMAScript (and a major difference from most programming languages) is that variables don’t have to be declared before being used. For example:

In the previous code, sTest is declared with a string value of “hello”. The next line uses a variable named sTest2 to create a concatenation of sTest and the string “world”. The variable sTest2 hasn’t been defined using the var operator; it has just been inserted as if it has already been declared.

When the ECMAScript interpreter sees an identifier that hasn’t been declared, it creates a global variable with the given name of the identifier and initializes it with the value specified. This is a handy feature of the language, but it can also be dangerous if you don’t keep track of variables closely. Best practice is always to declare all variables as you would with other programming languages.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Java Script Topics