WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[next]

Primitive Data Types, Arrays, Loops, and Conditions
Part 2

Stoyan Stefanov

Digg This Add to del.icio.us

Subject Matter Expert - Managed Services (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume


Primitive Data Types

Any value that you use is of a certain type. In JavaScript, there are the following primitive data types:

  1. Number—this includes floating point numbers as well as integers, for example 1, 100, 3.14.
  2. String—any number of characters, for example "a", "one", "one 2 three".
  3. Boolean—can be either true or false.
  4. Undefined—when you try to access a variable that doesn't exist, you get the special value undefined. The same will happen when you have declared a variable, but not given it a value yet. JavaScript will initialize it behind the scenes, with the value undefined.
  5. Null—this is another special data type that can have only one value, the null value. It means no value, an empty value, nothing. The difference with undefined is that if a variable has a value null, it is still defined, it only happens that its value is nothing. You'll see some examples shortly.

Any value that doesn't belong to one of the five primitive types listed above is an object. Even null is considered an object, which is a little awkward—having an object (something) that is actually nothing. We'll dive into objects in Chapter 4, but for the time being just remember that in JavaScript the data types are either:

Finding out the Value Type —the typeof Operator

If you want to know the data type of a variable or a value, you can use the special typeof operator. This operator returns a string that represents the data type. The return values of using typeof can be one of the following—"number", "string", "boolean", "undefined", "object", or "function". In the next few sections, you'll see typeof in action using examples of each of the five primitive data types.

Numbers

The simplest number is an integer. If you assign 1 to a variable and then use the typeof operator, it will return the string "number". In the following example you can also see that the second time we set a variable's value, we don't need the var statement.

Numbers can also be floating point (decimals):

You can call typeof directly on the value, without assigning it to a variable first:

Octal and Hexadecimal Numbers

When a number starts with a 0, it's considered an octal number. For example, the octal 0377 is the decimal 255.

The last line in the example above prints the decimal representation of the octal value. While you may not be very familiar with octal numbers, you've probably used hexadecimal values to define, for example, colors in CSS stylesheets.

In CSS, you have several options to define a color, two of them being:

In JavaScript, you put 0x before a hexadecimal value (also called hex for short).


[next]

Recent Articles

WebReference.com site name
Building a Banking Application Home Page with OOP
Mixing Scripting Languages
Review: phpFox, a Social Networking CMS with all the Bells and Whistles
internet.com site name
Enterprise 2.0: Social Networking in the Cloud
BroadSoft Marketplace Hastens Pace of Telephony Innovation
Review: HTC Hero for Sprint


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs