Native Types

Contact Us or call 1-877-932-8228
Native Types

Native Types

In addition to primitive data types, JavaScript provides a few other data types, which are implemented as objects.


We can store date values using Date objects. The Date object stores the date and time information internally as the number of milliseconds since January 1, 1970.

There aren't date literals in the language, so we have to explicitly create a Date object when we need one.

Code Sample:

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

	// current date and time:
	var rightNow = new Date(); 
	// 4th of July, note the 0-based month number:
	var holiday = new Date(1776, 6, 4); 
	// 4th of July, format varies with browser locale (avoid this):
	var holiday2 = Date.parse('7/4/1776'); 
---- C O D E   O M I T T E D ----

There two important pitfalls in the above example:

  1. The month is a number from 0 to 11 when passed as a parameter.
  2. The parseable string formats vary by browser implementation and by user locale, so avoid the parse() method.


Arrays are very powerful in JavaScript.

The Array object can be instantiated using a constructor call or a literal. The array indices must be positive integers, but the values stored in an array can be of any type or reference to any object.

We can create new arrays using the new Array constructor or a literal (using square brackets).

Code Sample:

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

	//constructor call
	var cities = new Array();
	cities[0] = "Albuquerque";
	cities[1] = "Syracuse"; 
	alert(cities.length); //cities.length will be 2
	cities[9] = "Tampa"; //cities.length will be 10
	 //literal syntax
	var teams = [ "Cubs","Yankees","Mariners" ];
	alert("Go " + teams[1] + "!");  //Will alert the string: "Go Yankees!"
	alert(teams.length); // length will be 3
	 //literal syntax with not initial values
	var dogs = [];
	alert(dogs.length); // length will be 0
---- C O D E   O M I T T E D ----

Things to note:

  1. The array index starts at 0 in JavaScript
  2. Arrays can be initialized with values or without values using the literal syntax.
  3. The array length is always one greater than the highest numerical index. You should ensure your indices are continuos positive numbers if you are going to loop over the values in the array using the for (var i=0; i<myarray.length; ++i) {} approach.


The Object type serves as the base for all the objects in JavaScript, regardless of their data type or how they were created.

The Object type is also used when we want to create custom objects. As with arrays, we can create new objects using a constructor call or a literal (using curly brackets).

Code Sample:

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

	//constructor call
	var employee = new Object(); = "Homer Simpson";
	employee.badgeNumber = 35739;
	//literal syntax
	var boss = {
		"name" : "Montgomery Burns",
		"badgeNumber" : 1
	employee.reportsTo = boss;
	alert( + " reports to " +;
---- C O D E   O M I T T E D ----

Regular Expressions

Regular Expressions are used to find occurrences of a string pattern inside a larger string.

Learning regular expressions can have a big payoff depending on the type of work you do.

JavaScript implements regular expressions with RegExp objects. It also supports the Perl-style literals.

Code Sample:

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

	var text = "Webucator";
	var pattern = new RegExp('cat', 'g');
	var samePattern = /cat/g; //using the literal syntax
	alert( pattern.test( text ) );// shows 'true'
	alert( samePattern.test( text ) );// shows 'true' again
---- C O D E   O M I T T E D ----

We will study regular expressions in a later lesson.