JavaScript Literals

by George Freedrich.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on ajax and javascript  

You are here: Categories » » AJAX and JavaScript

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.

Numbers

The 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 Notations

If 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 Literals

Base 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.

Leave a comment or ask a question
Total comments: 0

AJAX and JavaScript Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
What Is Ajax - Ajax stands for Asynchronous Javascript And XML. Although strictly speaking Ajax itself is not a technology, it mixes well-known programming techniques in an uncommon way to enable web develo (more...)
JavaScript as a Primary Development Language for AJAX applications - JavaScript is a powerful scripting language, but deserved or undeserved, it has gained a bad reputation. If you take some time to look at JavaScript with a fresh eye, you will notice that most (more...)
Promises and Problems of Combining AJAX with Other New Technologies - As you work with AJAX, you may hear of related technologies that you can use with AJAX. They fit into two main groups: mature technologies that are widely available in many browsers today, and (more...)
XMLHttpRequest Overview - Originally, Microsoft designed XMLHttpRequest to allow Internet Explorer (IE) to load XML documents from JavaScript. Even though it has XML in its name, XMLHttpRequest really is a generic HTTP (more...)
Goals of AJAX - First and foremost, AJAX is about improving user experience; user experience improvements fall into two categories: making current tasks easier and making previously impossible tasks possible. (more...)
JavaScript Lives in a Web Page - All the code that you write for JavaScript goes into an HTML page. If you don't know HTML yet, you should run out and get a good book on HTML. Lynda and William Weinman's (more...)
JavaScript Variables - I like to think of variables as containers on a container ship. You can put all different types of content into the containers, move them to another port, empty them, and then replace the contain (more...)
JavaScript Operators - Operators can be placed into three categories-binary, unary, and ternary. Binary operators, most commonly associated with the concept of operator, take two (binary) expressions and combine them in (more...)
JavaScript Arrays - Because objects are collections of properties with each property having its own name and value, arrays are actually JavaScript objects. Each property in an array is an element, and each element (more...)
Loops in JavaScript - Loops in JavaScript are similar to loops in C++ and Java and most other languages using loop structures. In this section, you will find explanations of the different types of loops in JavaScript (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.