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:
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:
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.
Java Script Related Tutorials
|Adv Java Tutorial||Core Java Tutorial|
|Java-Springs Tutorial||Java Servlets Tutorial|
|EJB(Enterprise JavaBeans) Tutorial||JavaServer Faces (JSF) Tutorial|
|Java Swing Tutorial||Java Tutorial|
|JavaMail API Tutorial||Java 8 Tutorial|
|Java XML Tutorial||Java Bean Utils Tutorial|
|The Java Debugger (JDB) Tutorial||Java.math Package Tutorial|
Java Script Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java-Springs Interview Questions|
|Java Servlets Interview Questions||EJB(Enterprise JavaBeans) Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java Swing Interview Questions|
|Java 8 Interview Questions||Java XML Interview Questions|
|JavaFX Interview Questions||The Java Debugger (JDB) Interview Questions|
Java Script Related Practice Tests
|Java Script Practice Tests||Adv Java Practice Tests|
|Core Java Practice Tests||Java-Springs Practice Tests|
|Java Servlets Practice Tests||EJB(Enterprise JavaBeans) Practice Tests|
|JavaServer Faces (JSF) Practice Tests||Java 8 Practice Tests|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.