Changes

Jump to: navigation, search

JavaScript Variable Types

1,035 bytes added, 20:15, 11 April 2007
no edit summary
<td>object<td>document, window<td>JavaScript provides a set of predefined objects to which the JavaScript programmer has access. For example the ''document'' object refers to the current web page and can be accessed to make changes to the page content.
</table>
 
JavaScript sets the variable type based on the value assignment. For example whne JavaScript encounters the following code it knows that ''myVariable'' should be of type ''number'':
 
<pre>
var myVariable = 10;
</pre>
 
and that the following variable type is ''string''.
 
<pre>
var myVariable = "Hello World!";
</pre>
 
JavaScript is also mcuh more flexible than many other programming languages. With languages such as Java a variable must be declared to be a particular type when it is created and once created, the type cannot be changed. JavaScript, on the other hand, allows the type of a variable to changed at any time simply by assigning a value of a different type. The following example is perfectly valid use of a variable in javaScript. At creation time, the variable is clearly of type ''number''. A later assigment of a ''string'' to this variable changes the type from ''number'' to ''string''.
 
<pre>
var myVariable = 10;
myVariable = "This is now a string type variable";
</pre>
 
== The JavaScript ''typeof'' method ==

Navigation menu