JavaScript Data Types

JavaScript is a loosely typed language and so converting data from one type to another will often happen without your having to specifically request it. Having the wrong field convert to a different type is one possible cause of scripts not doing what you might otherwise expect (for example a number might be converted to a string resulting in the + operator concatenating the values instead of adding them).

The simplest data types in JavaScript are undefined and null each of which only has one value that you either can't change (with null) or shouldn't change (with undefined). The value assigned to any variable if you don't explicitly assign one is undefined.

The simplest type you can actually assign a value to yourself is boolean. This data type has two values - true and false (note that these values should not have quotes around them as that would be string values rather than boolean values). JavaScript uses boolean values to determine whether or not to run certain code via if statements and similar. In most cases the boolean values are not specifically stored in variables but are instead generated on the fly by performing a comparison between fields of other types.

Javascript has only the one data type to store numbers and so doesn't really care whether the numbers are integers, are supposed to have a set number of decimal places, or are floating point. Because of this it may not be as obvious to beginners how calculations involving numbers that are not integers can sometimes produce results different from what they expect simply because they haven't realised that the calculations have resulted in numbers that are not integers. As with all programming languages, JavaScript stores numbers internally in binary and so fractions that are not exact divisions by two are only able to be stored as approximations and hence may differ from the true value by a very small amount - which is enough to make two numbers you'd expect to be equal not be.

Text in JavaScript is stored in strings. Anything defined between quotes in JavaScript is a string (with the exception of "use strict"; which is a JavaScript command masquerading as a string so that it doesn't cause syntax errors in older browsers. JavaScript makes no distinction between ' and " when used around strings as long as you use the same on for both the start and end of the string. The only time it makes any difference is where you also have one or both of them inside the string itself where the one used as the delimiter needs to be preceded by \ inside the string in order to avoid prematurely terminating the string.

Everything else in JavaScript is an object. JavaScript contains a number of built in object types that you can use as the basis for creating your own objects and data types. The objects built into JavaScript are:

In the case of those objects that have data type equivalents the conversion from the original data type into the object equivalent will be provided automatically when you apply one of the object's methods to the data. You should never need to actually create objects of those types directly.

Static objects are intended to be used directly. The other objects are intended to be used the way you would use a class in an object oriented language - as the basis for creating your own objects.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow