| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |||||||||||||||||||||
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
Data TypesThese tutorials are about JavaScript and its use for client-side Web programming. Data Typesdata type: a general term to describe categories of values that a program or programming language can make use of A computer works with values. It manipulates values and produces some kind of result. Depending on the program, it may be limited as to what types of values it can work with. The types of values a program can work with are known as its data types. JavaScript supports many types of data. Data types can be broken into two broad categories, primitive and composite. A primitive data type is a data type that stores a single value, normally a literal of some sort, such as numbers and strings. A composite data type, for the purposes of JavaScript, is the same thing as an object. It is a data type that can consist of multiple values grouped together in some way. JavaScript treats objects as associative arrays. An associative array is an array that can have its elements associated with names in addition to their numeric index position in the array. JavaScript also treats functions as objects. This means that functions can be used just like any other variable. Primitive Data TypesThere are three primitive data type of interest to us:
NumbersNumbers are the easist of the data types to understand. They represent numeric values. The simplest type of number is an integer. An integer is a base-10 whole number. In other words, it is a number with no fractional component. JavaScript can handle integer values between 253 and -253. Some JavaScript functions, however, cannot handle extremely large or small numbers, so it is probably a good idea to try to keep your numbers in the range of 231 and -231. Since that is plus or minus 2 trillion, this shouldn't be a problem as long as you are not trying to track the national debt. All of the following are valid integers.
Another commonly used type of number is a floating-point number. A floating-point number is a number that is assumed to have a decimal point. The decimal point is the point that floats and gives the data type its name. Floating-point numbers have a fractional component, even if that fractional component is zero. They can be represented as a real number (integer - decimal point - fractional value) such as in 3.1714, or using exponential notation. Exponential notation is a number multiplied by ten times the power of some exponent, for instance 3.1714x10-15 (0.0000000000000031714). Exponential notation is usually written with an "e" instead of the "x10", so that JavaScript will know it is a floating-point number and not an equation. This would read as 3.1714e-15. All of the following are valid floating-point numbers.
JavaScript stores floating-point numbers in exponential form, storing the number and its exponent. Just to confuse the issue, it also stores integers as floating-point numbers, but this is transparent to you as the programmer and you don't have to worry about it. Being a computing language, JavaScript also has the ability to handle hexadecimal (four bit) and octal (three bit) numbers. Octal support is newer and is best avoided since older version of JavaScript will think the octal number is a hexadecimal number. All hexadecimal numbers begin with a zero and an X, for instance 0xff0000. Octal numbers, if they are supported, begin with a zero, for instance, 0755. If they are not supported, this notation will be treated as another way of expressing hexadecimal numbers. JavaScript also has some numeric keyword literals you should know about. These are listed below.
StringsA string is sequence of valid characters within a given character set. It is normally used to represent text. A string literal is defined by enclosing it in matching single or double quotes.
If you create a string with nothing in it, it is called an
Some characters that you may want in a string may not exist on the keyboard, or may be special characters that can't appear as themselves in a string. In order to put these characters in a string, you need to use an escape sequence to represent the character. An escape sequence is a character or numeric value representing a character that is preceeded by a backslash ( Some escaped characters are as follows:
Boolean Values
There are two boolean values,
If you need to use boolean values in computations, JavaScript will automatically convert The conversion can also work the other way. When testing for the result of a comparison, JavaScript will treat any non-zero value as true, and a zero value as false. JavaScript will also test to false when you test for the existence of something that does not exist. Composite Data TypesAll composite data types can be treated as objects, but we normally categorize them by their purpose as a data type. For composite data types we will look at objects, including some special pre-defined objects that JavaScript provides, as well as functions and arrays. We will look at all of these elements in more detail elsewhere, so we will only touch on them lightly here. ObjectsAn object is a collection of named values, called the properties of that object. Functions associated with an object are referred to as the methods of that object.
Properties and methods of objects are referred to with a dot.notation that starts with the name of the object and ends with the name of the property. For instance
Normally in objects there are only two nodes, the object and the property, but as we will discover, sometimes the properties can have properties of their own, creating an object tree which you have to specify the path through in the same way you would with a directory tree (except you are using periods instead of slashes).
For instance,
Objects in JavaScript can be treated as associative arrays. This means that
For instance, this code snippet:
JavaScript has many predefined objects, such as a Date object and a Math object. These are used much as function libraries are used in a language like C. They contain a collection of useful methods that are predefined and ready for use in any JavaScript code you may write. FunctionsA function is a piece of code, predefined or written by the person creating the JavaScript, that is executed based on a call to it by name. Any JavaScript that is not inside a function (or is not associated with some even attribute or hyperlink attribute) is executed the moment the Web browser reaches it when first parsing the document. Functions allow execution to be delayed. They also allow for the same piece of code to be re-used many times in the same document, since functions allow the section of code they contain to be referred to by name. A function is a data type in JavaScript. This is different from many other programming languages. This means that they can be treated as containing values that can be changed. ArraysAn Array is an ordered collection of data values. In some programming languages, arrays have very specific limitations. In JavaScript, an array is just an object that has an index to refer to its contents. In other words, the fields in the array are numbered, and you can refer to the number position of the field.
The array index is included in square brackets immediately after the array name. In JavaScript, the array index starts with zero, so the first element in an array would be
JavaScript does not have multi-dimensional arrays, but you can nest them, which is to say, an array element can contain another array. You access them listing the array numbers starting for the outmost array and working inward. Therefore, the third element (position 2) of or inside the ninth element (position 8) would be We will be discussing how to create and work with arrays elsewhere. JavaScript Object Literals
JavaScript also has two keyword literals that it considers to be objects. These are
Something with the value
Unlike
An undefined variable is one that has been named, but does not have a value assigned to it. This means that it is not zero, it is not
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
| ||||||||||||||||||||