Skip to content

Latest commit

 

History

History
171 lines (111 loc) · 5.17 KB

File metadata and controls

171 lines (111 loc) · 5.17 KB
chapter section title attribution github
javascript-101
2
Types
jQuery Fundamentals
jquery

The types in JavaScript fall into two categories; primitives and objects. The primitive types include:

  • string
  • number
  • boolean
  • null
  • undefined

String

String types are text wrapped in single or double quotation marks, but it is best practice to stick with a consistent variation. There may be times when the string contains quotation marks that collide with the ones used to create the string; in this case we must either escape the characters using a \ backslash or use different quotes around the string.

var a = "I am a string"; var b = 'So am I!';

alert(a); alert(b);

var statement1 = 'He said "JavaScript is awesome!"'; var statement2 = "He said \"JavaScript is awesome!\"";

Number

Number types are just any positive or negative numeric value, there is no distinction between integer and floating point values.

var num1 = 100; var num2 = 100.10; var num3 = 0.10;

Boolean

Boolean types are just simply true or false.

var okay = true; var fail = false;

Undefined and Null

Undefined and null are special types in JavaScript. Null types are a value that represent the absence of a value, this is similar to many other programming languages. Undefined types represent a state in which no value has been assigned at all, you can achieve this type in two ways; by using the undefined keyword or by just not defining a value at all.

var foo = null;

var bar1 = undefined; var bar2;

Objects

Everything else is in JavaScript is considered an Object. While there are numerous built-in objects, the ones we will focus on in this chapter are:

  • Object
  • Array
  • Function

The simplist way to create an object is either through the Object constructor or the short hand syntax other wise known as an object literal. These simple objects are unordered key/value pairs; the key is formally known as a property and the value can be any valid JavaScript type, even another object. To create or access a property on an object, we use what is known as "dot notation" or "bracket notation".

var person1 = new Object;

person1.firstName = "John"; person1.lastName = "Doe";

alert(person1.firstName + " " + person1.lastName);

var person2 = { firstName: "Jane", lastName: "Doe" };

alert(person2.firstName + " " + person2.lastName);

var people = {};

people['person1'] = person1; people['person2'] = person2;

alert(people['person1'].firstName); alert(people['person2'].firstName);

What happens if a property is accessed which has not been defined yet? Well, it will be a type of undefined.

var person = { name: "John Doe" }; alert(person.email); // => undefined

Array

Arrays are a type of object which are ordered by the index of each item that it contains; this index starts at zero and extends to however many items have been added, also known as the "length" of the array which happens to be a property as well. Similar to a basic object, an array can be created with the Array Constructor or the short hand syntax known as an array literal.

var foo = new Array; var bar = [];

There is an important distinction to be made between the two though. An array literal can contain items to be added to the array upon creating it, the same is possisble for the Array Constructor. However, if just a single numeric item is passed in, the Array Constructor will assume its length to be that value.

var foo = [100]; alert(foo[0]); alert(foo.length);

var bar = new Array(100); alert(bar[0]); alert(bar.length);

An array can be manipulated through the methods that are avaiable on the instance and items can be accessed using bracket notation with a given index, the value will be undefined if the index does not exists or contains no value.

var foo = [];

foo.push('a'); foo.push('b');

alert(foo[0]); alert(foo[1]);

alert(foo.length);

foo.pop();

alert(foo[0]); alert(foo[1]);

alert(foo.length);

foo.unshift('z');

alert(foo[0]); alert(foo[1]);

alert(foo.length);

foo.shift();

alert(foo[0]); alert(foo[1]);

alert(foo.length);

There are many more methods for manipulating arrays, details can be found on the MDN Document