The raw data that make up the root of data types are called "literals." These are, in effect, literally what they represent themselves to be. Numbers, strings, and Boolean values make up the core set of literals in JavaScript. Little mystery exists with literals, but important differences exist between them. NumbersThe fundamental data in most computer languages are numbers. Because JavaScript is weakly typed, all numbers are treated as floating-point, so you need not distinguish between integers and floating-point literals. All of the following values are treated as numeric literals: 223.48 20 0 500.33 When assigning numeric literals to names (identifiers), you simply write them in their raw form, with no required quotation marks or other characters, to represent decimal values. With numbers other than decimal values or very large numbers, special requirements exist. Scientific NotationsIf you need scientific notations or are returned a value written in a scientific notation, you will be glad to know that they are written in standard format. For example, you might see the value 9.00210066295925e+21 on the screen after your script has calculated some really big numbers. The letter e is followed by a plus or minus sign and from one to three integers. (The sign is placed in a returned result but is optional if you write your own notation.) The integers following the e are the exponent, and the rest of the number (preceding the e notation) is multiplied by 10 to the power of the exponent. In most JavaScript applications, numbers with scientific notations do not appear, but if they do, they are treated for purposes of calculations just like any other number. Hexadecimal LiteralsBase 16 or hexadecimal literals have a special preface to alert the parser that the combination of numbers and letters is indeed made up of special values. All hexadecimal literals are prefaced by 0x (zero-x), followed by 0-9, A-F characters indicating a hexadecimal value. For example, the color red in hexadecimal is FF0000; in JavaScript, it is written as 0xFF0000. All calculations done in hexadecimal values in JavaScript are returned as decimal values. For example, if you add 0xa8 to 0xE3, the resulting value in decimal is 395 instead of the hexadecimal value 18b. Fortunately, JavaScript provides a way to express hexadecimal values using the toString( ) method. By including the number base as an argument, you can return a hexadecimal value. The following script shows how. hexNota.html<html> <head> <title> Hexadecimal Values </title> <script language="JavaScript"> var alpha=0xdead; var beta=0xbeef; var gamma=(alpha + beta).toString(16); document.write(gamma); </script> </head> <body bgcolor="springgreen"> </body> </html> Hexadecimal values' most familiar application in JavaScript and HTML is as sixcharacter color values. It requires no calculations for a color other than conversion from decimal. However, many other occasions might arise in which calculations using hexadecimal values occur, and knowing how to generate hexadecimal results in JavaScript can be useful.
|
|||||||||||||
Disclaimer
1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us or use the "Report this article" button on this page to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. |
|||||||||||||