Brian's Blog

items I see across my tribes

JavaScript: Data Types

June 13
by briancarter 13. June 2011 08:01

With HTML5 getting so much attention, I’ve decided to blog on the fundamentals of JavaScript over the next few blog entries.  When exploring any new language, the first thing I do is learn the data types. 

JavaScript is not a strongly typed language which means you rarely have to concern yourself with the type of data a variable is storing, only what the variable it is storing and in JavaScript, variables can store anything, even functions.  Even so, one should know about the primitive data types.

JavaScript has five primitive data types:

  • Boolean
  • Number
  • String
  • Null
  • Undefined

Boolean

A Boolean is a logical entity that consists of either a true or a false value. An example of one is:

var BooleanValue = true; 

There are two Boolean values, true and false. These are normally the result of a logical comparison in your code that returns a true/false result, such as

a == b
.

This statement reads: does the value of variable a equal the value of variable b?

JavaScript will treat any non-zero value as true, and a zero value as false. JavaScript will also test to false when you test for the existence of something that does not exist.

Number

A Number is a set of numerical digits that represent a number. We’ll use base-10 numbers. An example:

var NumericalValue = 354;

Numbers are the easiest of the data types to understand. They represent numeric values.

The simplest type of number is an integer.

An integer is a base-10 whole number. In other words, it is a number with no fractional component. JavaScript can handle integer values between 253 and -253. Some JavaScript functions, however, cannot handle extremely large or small numbers, so it is probably a good idea to try to keep your numbers in the range of 231and -231. Since that is plus or minus 2 trillion, this shouldn't be a problem as long as you are not trying to track the national debt.

All of the following are valid integers.

  • 0
  • -75
  • 10000000
  • 2

Another commonly used type of number is a floating-point number. A floating-point number is a number that is assumed to have a decimal point. The decimal point is the point that floats and gives the data type its name.

Floating-point numbers have a fractional component, even if that fractional component is zero. They can be represented as a real number (integer - decimal point - fractional value) such as in 3.1714, or using exponential notation.

Exponential notation is a number multiplied by ten times the power of some exponent, for instance 3.1714x10-15 (0.0000000000000031714). Exponential notation is usually written with an "e" instead of the "x10", so that JavaScript will know it is a floating-point number and not an equation. This would read as 3.1714e-15.

All of the following are valid floating-point numbers.

  • 5.00
  • 1.3333333333333
  • 1.0e25
  • 7.5e-3

JavaScript stores floating-point numbers in exponential form, storing the number and its exponent. Just to confuse the issue, it also stores integers as floating-point numbers, but this is transparent to you as the developer and you don't have to worry about it.

JavaScript also has some numeric keyword literals you should know about. These are listed below.

Infinity
Infinity has the value of infinity, which is to say a number larger than the largest number that JavaScript can represent. Not really infinity, but from a computing perspective, it might as well be.
There is also the keyword literal -Infinity for the negative infinity.
As well as keywords, these can be represented as properties of the Number object asNumber.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY.
Using the number object properties comes from older JavaScript, while the keyword literals were first implemented with JavaScript 1.3. You can test for infinity with the isFinite() function.
NaN
NaN is the value returned when you try to treat something that is Not A Number as a number. For instance the results of 7 times "abc" is not a number. You can test for not-a-number values with the isNaN() function.

String

A String is a set of zero or more characters. An example:

var StringValue = "This is a String";

A String is used to manipulate a stored piece of text.  A string is sequence of valid characters within a given character set. It is normally used to represent text. A string literal is defined by enclosing it in matching single or double quotes.

If you create a string with nothing in it, it is called an empty string. It is normally created by two quote marks with nothing between them.

As with algebra, JavaScript variables are used to hold values or expressions. A variable can have a short name, like x, or a more descriptive name, like carname. Rules for JavaScript variable names:

  • Variable names are case sensitive (y and Y are two different variables)
  • Variable names must begin with a letter or the underscore character

Note: Because JavaScript is case-sensitive, variable names are case-sensitive.

Null

The null value is a special value in JavaScript. The null value represents just that – Nothing. If you try to reference a variable that isn’t defined and therefore has no value, the value returned is the null value.

Null is an empty value. null is not the same as 0 -- 0 is a real, calculable number, whereas null is the absence of any value.

Undefined

The undefined property indicates that a variable has not been assigned a value.

I was reading a modern, popular book on JavaScript last night and was disappointed by the handling of null. The author started out doing a lot of checking like:

if (foo == null) {   alert('foo is not set.'); }

Then told the reader that they could just remove the == null because javascript knows you mean "== null"

What?! This isn't why you don't check for equality with null. It's because foo == null doesn't even remotely do what most people think it does in this context.

It's a commonly held belief that uninitialized properties in JavaScript are set tonull as default values. People believe this mostly for 2 reasons: 1) foo == null returns true if foo is undefined and 2) authors don't teach JavaScript properly.

A property, when it has no definition, is undefined. Put that way, it's pretty obvious.

null is an object. It's type is null. undefined is not an object, it's type is undefined. That part is less obvious.

The real trouble is that == does type coercion. === checks for both type and value and is the most intuitive form of equality in JavaScript, in my opinion.

So people write

    if (foo == null) {    foo = "Brian";
    }
    
When what they really mean is
    if (!foo) {    foo = "Brian";
    }

If you find yourself with a lot of null checks in your JavaScript, set aside some time and review Douglas Crockford's doc on The-Good-Parts.

Composite Data Types

All composite data types can be treated as objects, but we normally categorize them by their purpose as a data type. For composite data types we will look at objects, including some special pre-defined objects that JavaScript provides, as well as functions and arrays in the next blog entry.

Categories:


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.