JavaScript
  Home arrow JavaScript arrow Page 2 - The Power of Javascript: Basic Types of Da...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
eWeek
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

The Power of Javascript: Basic Types of Data
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 19
    2005-07-12

    Table of Contents:
  • The Power of Javascript: Basic Types of Data
  • Data as Numbers
  • Boolean Data, Yes or No
  • One Small Change

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    The Power of Javascript: Basic Types of Data - Data as Numbers
    (Page 2 of 4 )

    Numbers such as 12 or -12 are called integer numbers because they're whole numbers. Numbers such as 12.34 or -12.34 are called floating point numbers because these numbers contain a decimal point and a fraction value. In Javascript we have one data type called number that stores both the integer and floating point number values. Actually, Javascript stores all the numerical values as floating point values, so if you stored the value 1.00 it means 1 to Javascript (as an integer value) and when you add a fraction, for example 1.12, Javascript understands that it's a floating point value. The range of values that can be used for the number data type is very big, from ±5.0 × 10−324 to ±1.7 × 10308 for floating point numbers, and for integer numbers from –253 to 253. We will not need this range of values unless we are working on scientific applications.

    Data as Character Strings

    Like most programming languages, Javascript provides a data type that stores text as character strings and not as code. It's called a string because it's a row of characters. To define a string value you use the double quotation marks to delimit the string value like "Javascript is fun". You can use the single quotation marks instead of the double marks, so you could write it as 'Javascript is fun'. Note that you must match the opening and closing quotation marks so you can't write a string value like 'Javascript is fun" or "Javascript is fun'.

    When the Javascript interpreter encounters a value between double quotes (or single quotes) it deals with it as text, not code, so the value "34.43" is a string, not a number, and the value "var x = 34" is also a string, not Javascript code. Usually, you use string values to display messages to users and to gather user information and validate it along with other operations that we will be doing throughout the series. Suppose that you need to store the following value as a string:

    I'm a Javascript Programmer

    If you used the single quotes to delimit a string value, you would have something like this:

    'I'm a Javascript Programmer'

    This line produces an error because the interpreter thinks that the apostrophe in I'm is the closing single quote of the string, so to the interpreter the string consists of 'I' only. To solve this problem, use  double quotes to delimit the string, so it will be "I'm a Javascript Programmer". The same rule applies if you want to include a double quote inside the string value. You just have to use the single quotes to delimit string value that contains a double quote inside. We will look at an example after a while about all the data types.

    A string value must be written in one line. It can't span two or more lines, so you can't write a string value like this:

    "Hello guys,
    I'm learning Javascript"

    This produces an error. If you want to include a new line in your string value, you can use the escape character \n. There are many characters that can't be represented directly in a string value; for example, the copyright symbol and the new line character. Can you type these characters directly in your string value? No, and that's why most programming languages have the concept of escape characters, which are a special value that has a special meaning when used.

    An escape character consists of a backslash followed by a character. It is called an escape character because it doesn't appear (escape) in the string as you write it; instead it has a special meaning and functionality. For example, to include a new line inside your string value use the new line (\n) escape character, to include the copyright symbol in your string value use the Escape Character \xA9. Suppose that you have the string value "Hello\nGuys". When the Javascript interpreter encounters this value, it knows that it's a string value because of the double quotes around it. Then the interpreter searches for an escape character, so Hello is a string value, then it finds \n which means escape the value \n, put in a new line character, and then continue the string value. We will see an example covering escape characters shortly. The following are the available Escape Characters

    \'Single Quote Escape Character
    \"Double Quote Escape Character
    \nNew Line Escape Character
    \tTab Escape Character
    \rCarriage Return
    \fForm Feed
    \eBackspace
    \bEscape
    \\Backslash

     

     

     

     

     

     

     

     

     

    You can use the form \ddd to include a character with the Latin-1 Encoding using the Octal Base. You can also use the form \xdd to include a character with the Latin-1 Encoding, but this time using the Hexadecimal representation, where dd is the character code. Or you can use the form \xdddd to include a Unicode Character using the Hexadecimal representation, where dddd is the character code.

    More JavaScript Articles
    More By Michael Youssef


     

    JAVASCRIPT ARTICLES

    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...
    - Improving an Image Zooming Application with ...
    - Zooming in on Images with JavaScript
    - JavaScript Date Objects: Universal Coordinat...
    - Javascript Objects: More Date Methods
    - JavaScript Objects: Dates
    - JavaScript Objects: Finishing Strings

    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway