Thomas Valentine
JavaScript Data Types
February 2, 2009
|
This week we cover JavaScript Data Types. There are a number
of different types of data that JavaScript can work with.
The different types of data are Numbers, Strings, Boolean,
null, and undefined. Each is used in a slightly different
way for varying functionality. Understanding each data type
is of paramount importance when you begin to write more
complex scripts that require the use of more than one type
of data.
|
Data Types - Number
In JavaScript, every number data type is treated as a
floating-point number. A floating-point number is a number
that includes a decimal place. For example, the fraction
one-half wouldn't be written as .5, but as 0.5. Multiply
that 0.5 by 6.1234 (or any number, really), and the decimal
(the "point") will have "floated" to a new position within
the new number. Hence the term "Floating Point". Because of
this floating-point capability, JavaScript is great to use
with numbers. Combine this with its loosely typed nature and
you get a very easy to use and flexible yet powerful
programming language.
Within the Numbers data type, there are a few different
variations that must be addressed. Understanding these
variations is crucial to your skill in working with them. As
with most concepts within JavaScript, they are easy to
understand. Along with the different number types are
several built in number values that can be used within your
scripts. The different types and built in values are as
follows.
Integers
An integer is a number that exists without a decimal place.
It is a whole number. For example, the number 5 would be an
integer, but the number 5.5 would not be due to the
existence of a decimal place. An integer may be positive or
negative. An integer may be stated as a decimal, octal, or
hexadecimal number. The below list explains what is meant by
the terms decimal, octal, and hexadecimal.
- Decimal - A decimal number is any number that
exists with a base number of 10. The decimal number is what
we (humans) use in every day numbering. It is represented by
the numbers (or combinations of) 0 to 9.
- Hexadecimal - A Hexadecimal number is a number
that exists with a base number of 16. The hexadecimal
numbering system is widely used within the computing
industry because a large value may be stated using a small
amount of numbers. It is represented by the numbers 0 to 9
(which represent the decimal numbers 0 to 9) and the letters
A to F (which represent the decimal numbers 10 to 15). A
hexadecimal number must begin with 0x or 0X when stated
within JavaScript. An example of a hexadecimal number stated
within JavaScript is 0XFF that represents the decimal number
255.
- Octal - An Octal number is a number that exists
with a base number of 8. The Octal numbering system isn't
used within the computer industry as much as the hexadecimal
numbering system, but it still finds some applications. It
is represented by the numbers 0 to 7 and is stated within
JavaScript with a zero following the actual number value.
For example, the number 6 in octal must be stated as 06. An
example of an octal number stated within JavaScript is 077
that represents the decimal number 63.
You'll get a feel for thinking in these differing number
systems with some practice. Don't feel discouraged if you
don't catch on quickly. Thinking in a number system other
than ten is extremely difficult for a human.
Floating Point Numbers
As explained earlier, a floating-point number is a number
that contains a decimal. A Floating-point number may also be
expressed through the means of exponential notation. An
exponential number may be expressed with the base number
followed by an e or E, followed by the power you wish to
raise the number to. For example, the number 8 may be
expressed as the decimal number 2 raised to the third power
as such: 2E3.
Built In Values
There are a number of different built in values you may have
a use for. Use these values with the Math object to achieve
your desired result. You'll learn more about the Math object
later, for now what is important now is that you know they
exist. The different built in values are as follows.
- Math.E - This represents the base of natural
logarithms, also called Euler's Constant, which is
2.718281828459045 as a decimal floating point number.
- Math.LN2 - This represents the value for the
natural logarithm of 2, which is 0.6931471805599453 as a
decimal floating-point number.
- Math.LN10 - This represents the value for the
natural logarithm of 10, which is 2.302585092994046 as a
decimal floating point number.
- Math.LOG2E - This represents the value for the
base 2 of the log of E. E represents Euler's Constant, which
is 2.718281828459045 as a decimal floating point number.
- Math.LOG10E - This represents the value for the
base 2 of the log of E. E represents Euler's Constant, which
is 2.718281828459045 as a decimal floating point number.
- Math.PI - This represents the value of PI, which
is 3.141592653589793 as a decimal floating point number.
- Math.SQRT2 - This represents the square root of
2, which is 1.4142135623730951 as a decimal floating-point
number.
- Math.SQRT1_2 - This represents the square root of
one half, which is 0.7071067811865476 as a decimal floating-
point number.
You may use the above Math functions alone or together
within your JavaScript functions. For a more detailed look
at each of the above Math object properties, see the
JavaScript Language Reference, Object Properties. Special
Values
JavaScript has provided five different properties of the
Number object to further its capabilities as a math
intensive language. They are quite common in the world of
mathematics, but not so common in the world of programming.
They are as follows.
- Number.MAX_VALUE - This property is used to return
the largest number possible available to JavaScript. This
number is 1.7976931348623157e+308.
- Number.MIN_VALUE - This property is used to return
the smallest number possible available to JavaScript. This
number is 5e-324, which is five to the negative 324, which
is 5 with 324 zeros in front of it, then a decimal point.
- Number.NaN - This property is used to signify that
the value is NaN, which stands for Not A Number. It simply
has no other value but NaN. It is equal to no other number,
including itself.
- Number.POSITIVE_INFINITY - The POSITIVE_INFINITY
property is returned when a calculation returns a number
greater than the largest Positive number available to
JavaScript, which is 1.7976931348623157e+308.
- Number.NEGATIVE_INFINITY - The NEGATIVE_INFINITY
property is returned when a calculation returns a negative
number greater than the largest negative number available to
JavaScript, which is 5e-324.
You may use the above Number functions alone or together
within your JavaScript functions. For a more detailed look
at each of the above Number object properties, see the
JavaScript Language Reference, Object Properties.
Semi-Colon
JavaScript Introduction
Data Types - Strings
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript
Part 12: Getting to Know Dynamic HTML
|