hide - Understanding jQuery
hide - Understanding jQuery
hide - Understanding jQuery
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Even though it is possible to omit the var keyword from a variable definition, I recommend to<br />
always define your variable with the var keyword:<br />
var apples = 1;<br />
Let’s take a look at some of the basic principles behind variable definitions:<br />
1. A variable is often (but not always) a temporary storage of computer program data.<br />
2. Variables are used to store and manipulate data during program life cycle.<br />
3. The var keyword can be ommited from definition. But doing so is not recommended.<br />
4. The type of a JavaScript variable can be integer, string, boolean, function, array or JSON.<br />
5. In JavaScript a variable is also an object.<br />
6. The variable allocates enough physical computer memory to store the specified value.<br />
Variables are not unique to JavaScript. In other languages variables are created using all kinds<br />
of keywords. In PHP a variable name is prepended a dollar sign as in $apple. In C++, each<br />
different type of a variable has its own keyword. For example int for an integer type, char for<br />
a text character or short for “half ” a variable which is 2 bytes in length. 4 bytes would be a full<br />
32 bit set. On a 64-bit computer processor, an integer is 64-bit long.<br />
In JavaScript we use the var keyword to define a variable of any type. How does JavaScript<br />
determine the type of a variable being created? The type is defined by the assigned value. If we<br />
say that apples = 1, JavaScript automatically creates a variable of type integer.<br />
Determining Variable Type<br />
JavaScript variable definitions are typeless. This means that we can create any type of a variable<br />
and during the program lifecycle we can overwrite it by assigning a new value to it. This new<br />
value doesn’t have to be of the same type as the original. In comparison, this wouldn’t be true<br />
in languages like C++, where a variable of type integer can only be assigned an integer value.<br />
Typeless variables such as ones used by JavaScript can be reassigned, or turned into a different<br />
type using the equal sign. In C++ this process is known as type casting and requires manual<br />
action. But this is completely legal in JavaScript and happens automatically. To check the type<br />
of a variable at any point of its existence, we can use the typeof function:<br />
Parameter Returned result (as string)<br />
typeof(1) “number“<br />
typeof(“hello“) “string”<br />
typeof(true) “boolean”<br />
typeof(object) “object”<br />
typeof(null) “null”<br />
typeof(orange) “undefined” given that the variable named orange does not exist (was never defined)<br />
25