Home arrow JavaScript arrow Page 2 - The Power of Javascript: Basic Types of Data

The Power of Javascript: Basic Types of Data

Javascript interpreters understand two different, basic types of data: numbers and character strings. But interpreters only understand these data types when they are presented in certain ways. This article, the third in a series, will explain what these types of data are, and how to handle them.

Author Info:
By: Michael Youssef
Rating: 4 stars4 stars4 stars4 stars4 stars / 30
July 12, 2005
  1. · The Power of Javascript: Basic Types of Data
  2. · Data as Numbers
  3. · Boolean Data, Yes or No
  4. · One Small Change

print this article

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










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.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials