JavaScript Variable Types

From Techotopia
Revision as of 18:05, 1 February 2016 by Neil (Talk | contribs) (Text replacement - "<google>ADSDAQBOX_FLOW</google>" to "<htmlet>adsdaqbox_flow</htmlet>")

Jump to: navigation, search
PreviousTable of ContentsNext
Introducing JavaScript VariablesJavaScript Operators


<google>BUY_JAVASCRIPT</google>


The objective of this chapter is to provide an overview of JavaScript variable types.

JavaScript Variables

JavaScript supports five different types of variable. These variable types are outlined in the following table together with examples and a brief description of each type:

TypeExampleDescription
booleantrue, falseThe boolean variable is used to record a value of either true or false. Internally this is essentially stored as 1 for true and 0 for false.
number1, -31, 0.023The number variable holds any type of number, either an integer or a real number.
string"Hello World!"The string variable type stores a string of characters, typically making up either a word or sentence. string variables can also be assigned empty strings.
functionany built-in or user defined functionA function is one of the basic building blocks of most programming languages including JavaScript. Functions provide a way to organize functionality into clean, reusable modules that can be accessed from any other JavaScript to perform specific tasks. In addition to user created functions, JavaScript contains a number of built-in functions that provide pre-defined functionality. Functions are covered in Understanding JavaScript Functions.
objectdocument, windowJavaScript 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.

JavaScript sets the variable type based on the value assignment. For example when JavaScript encounters the following code it knows that myVariable should be of type number:

var myVariable = 10;

Similarly, JavaScript will detect in the following example that the variable type is string:

var myVariable = "Hello World!";

JavaScript is also much 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. This is referred to as strong typing. JavaScript, on the other hand, allows the type of a variable to be changed at any time simply by assigning a value of a different type (better known as loose typing). The following example is perfectly valid use of a variable in JavaScript. At creation time, the variable is clearly of type number. A later assignment of a string to this variable changes the type from number to string.

var myVariable = 10;
myVariable = "This is now a string type variable";

The JavaScript typeof Operator

The flexible nature of JavaScript variable types can lead to confusion as to the particular type of a variable at any given time in a script. To address this concern JavaScript provides the typeof operator. The typeof operator returns the current variable type of the specified variable. For example:

var myVariable = 10;
document.writeln ( "myVariable = " + typeof myVariable );

myVariable = "Hello";
document.writeln ( "myVariable = " + typeof myVariable );

When executed, the above code displays the following output:

myVariable = number
myVariable = string


<google>BUY_JAVASCRIPT_BOTTOM</google>



PreviousTable of ContentsNext
Introducing JavaScript VariablesJavaScript Operators