Primitive data types

Contact Us or call 1-877-932-8228
Primitive data types

Primitive data types

JavaScript comes with a number of data types that we can use in our variables. Let's look at them.


Null is a data type that has only one possible value, null, which is also a reserved word in the language. We use null to represent a value that we don't know or that is missing.

var name = "Homer"; var ssn = null;

In the above example we know what to put in the name variable, but we don't know yet what to put in the ssn variable. Maybe we will know what to put in there later in our program, but maybe not.


The Undefined type also has a single value, undefined, and it is similar to null but not exactly the same thing.

JavaScript uses undefined as the default value for any variable that has not been initialized yet. Let's modify our previous example.

var name = "Homer"; var ssn;

Now the value of ssn is undefined because it is no longer initialized to null or anything else.

The undefined type is used a lot when we want to detect if a variable has already been declared.

//Check if we already have a start time
if (startTime === undefined) {
	startTime = new Date();


Boolean is a very common data type in every language. It has only two values, true and false, which are reserved words and, I hope, self-explanatory.

var enabled = true;
var disabled = false;


The Number data type can represent two types of numeric values: 32-bit integers or 64-bit floating point numbers.

Number values are created using number literals, which are shown in the following demo.

Code Sample:

---- C O D E   O M I T T E D ----

	var numbers = {};
	numbers['age'] = 25; // simple, decimal, integer
	numbers['price'] = 45.95; // floating point
	numbers['letter'] = NaN; // not a number
	// (note leading 0) integer in octal, 509 in decimal:
	numbers['permissions'] = 0775; 
	// integer in hexadecimal, 28 in decimal:
	numbers['flags'] = 0x00ff; 
	// floating point in scientific notation:
	numbers['measurement'] = 5.397e-9; 

---- C O D E   O M I T T E D ----

for (var i in numbers) {
	document.write("<li><strong>" + i + ": </strong>");
	document.write(" is a " + typeof numbers[i]);
---- C O D E   O M I T T E D ----

This will display as follows: Numbers

  • The first two, 25and 45.95, are self-explanatory.
  • The third, NaN, stands for "Not a Number," which means that JavaScript cannot convert the value to a number.
  • The final three (octal, hexadecimal, and scientific notation) are special cases that you can use, but are unlikely to run into.


The String is used to represent text. Programmers spend a lot of time manipulating strings in pretty much any programming language.

We create strings using literal values enclosed in single or double quotation marks. JavaScript also supports a few special encodings for common characters such as new line (\n) and tab (\t).

Code Sample:

---- C O D E   O M I T T E D ----

<script type="text/javascript">
      * the backslash (\) is used as an escaped character in JS
      * \n = new line
      * \t = tab
      * \' = single quote
      * \" = double quote
      * \\ = backslash
      * \r = carriage return
       var chars = ['\n','\t','\',"\"",'\\','\r'];
---- C O D E   O M I T T E D ----
<script type="text/javascript">
for (var i in chars) {
	document.write("</span>, ");
---- C O D E   O M I T T E D ----

We use the <pre> tag so that the browser displays the new lines and tabs. The screenshot below shows how this will render. We've added arrows to indicate the location of the different characters: Location of Different Characters

Every value in JavaScript can be converted to a string by using the toString() method, like: var s = myValue.toString();.