Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1afacaa
style conformance for about-jquery/how-jquery-works
Nov 7, 2012
a98b0bf
style conformance for javascript-101/getting-started
Nov 7, 2012
776c4aa
style conformance for javascript-101/syntax-basics
Nov 7, 2012
f7641f4
style conformance for javascript-101/running-code
Nov 7, 2012
87ce1f2
style conformance for javascript-101/types
Nov 8, 2012
74be2db
style conformance for javascript-101/operators
Nov 8, 2012
cc01ee8
style conformance for javascript-101/conditional-code
Nov 8, 2012
2b4cf16
found the section on truthy/falsy
Nov 8, 2012
70b2003
style conformance for javascript-101/loops
Nov 8, 2012
6b04bb6
style conformance for javascript-101/arrays
Nov 8, 2012
0d7b57e
adding links to objects/arrays sections
Nov 8, 2012
f97c941
style conformance for javascript-101/objects
Nov 8, 2012
47fda0e
style conformance for javascript-101/functions
Nov 8, 2012
e498797
style conformance for javascript-101/testing-type
Nov 8, 2012
f1c8aff
link to testing-type section
Nov 8, 2012
2254121
style conformance for javascript-101/this-keyword
Nov 8, 2012
2dfb23a
style conformance for javascript-101/scope
Nov 8, 2012
1cb18ad
style conformance for javascript-101/closures
Nov 8, 2012
4259929
horizontal scrolling issues
Nov 8, 2012
a709de1
consistency with —
Nov 8, 2012
adcbaeb
style conformance for using-jquery-core/dollar-object-vs-function
Nov 8, 2012
bbe819e
style conformance for using-jquery-core/document-ready
Nov 8, 2012
bc6b21c
style conformance for using-jquery-core/avoid-conflicts-other-libraries
Nov 8, 2012
24b91ce
style conformance for using-jquery-core/attributes
Nov 8, 2012
8989211
style conformance for using-jquery-core/selecting-elements
Nov 8, 2012
d8c1ff5
style conformance for using-jquery-core/working-with-selections
Nov 9, 2012
3f9e54a
style conformance for using-jquery-core/manipulating-elements
Nov 9, 2012
947dff6
style conformance for using-jquery-core/data-methods
Nov 9, 2012
b17307b
style conformance for using-jquery-core/utility-methods
Nov 9, 2012
40c11cf
style conformance for using-jquery-core/jquery-object
Nov 9, 2012
96f06d4
style conformance for using-jquery-core/traversing
Nov 9, 2012
a52fe58
style conformance for using-jquery-core/css-styling-dimensions
Nov 9, 2012
6256c9e
style conformance for using-jquery-core/iterating
Nov 9, 2012
8f96503
style conformance for using-jquery-core/understanding-index
Nov 9, 2012
e512536
deleting obsolete exercises.md page
Nov 9, 2012
d6d5dcb
Update config-sample to reflect local.* instead of dev.*
ajpiano Nov 9, 2012
4ae27eb
Upgrade to grunt-jquery-content 0.5.10, add grunt-check-modules to gr…
ajpiano Nov 9, 2012
495a7b0
README.md updates
ajpiano Nov 9, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
style conformance for javascript-101/types
  • Loading branch information
jorydotcom committed Nov 8, 2012
commit 87ce1f2b18243800df29489d89ad15a4c13908a0
85 changes: 46 additions & 39 deletions page/javascript-101/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ attribution:
- jQuery Fundamentals
---

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

* string
* number
* boolean
* null
* undefined
* 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.
Strings are text wrapped in single or double quotation marks. It is best practice to consistently use one or the other. There may be times when the string contains quotation marks that collide with the ones used to create the string. In this case, either escape the characters using a `\` backslash or use different quotes around the string.

```
// Strings can created with double or single quotes.
Expand All @@ -37,7 +37,7 @@ 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.
Number types are any positive or negative numeric value. There is no distinction between integer and floating point values.

```
// Numbers are any whole or floating point integer.
Expand All @@ -47,45 +47,46 @@ var num3 = 0.10;
```

### Boolean
Boolean types are just simply true or false.
Boolean types are either true or false.

```
// Boolean values.
var okay = true;
var fail = false;
```

### Undefined and Null
### Null and Undefined

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.
Null and undefined are special types in JavaScript. Null types are a value that represent the absence of a value, similar to many other programming languages. Undefined types represent a state in which no value has been assigned at all. This type is created in two ways: by using the undefined keyword or by not defining a value at all.

```
\\ Two ways to achieve an undefined value.
// Two ways to achieve an undefined value.
var foo = null;

var bar1 = undefined;
var bar2;
```

### Objects
## Objects

Everything else is in JavaScript is considered an Object. While there are [numerous built-in objects](https://developer.mozilla.org/en/JavaScript/Reference#Global_Objects, "MDN - Global Object Reference"), the ones we will focus on in this chapter are:
Everything else is in JavaScript is considered an Object. While there are [numerous built-in objects](https://developer.mozilla.org/en/JavaScript/Reference#Global_Objects, "MDN - Global Object Reference"), this chapter will cover:

* Object
* Array
* Function

The simplest 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".
The simplest way to create an object is either through the Object constructor or the shorthand syntax known as 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."

```
// Simple objects using the constructor or the literal syntax.
// Creating an object with the constructor:
var person1 = new Object;

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

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

// Creating an object with the object literal syntax:
var person2 = {
firstName: "Jane",
lastName: "Doe"
Expand All @@ -105,7 +106,7 @@ 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.
If a property is accessed that has not been defined, it will return a type of undefined.

```
// Properties that have not been created are undefined.
Expand All @@ -116,65 +117,71 @@ 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.
Arrays are a type of object that are ordered by the index of each item it contains. The index starts at zero and extends to however many items have been added, which is a property of the array known as the "length" of the array. Similar to a basic object, an array can be created with the array constructor or the shorthand syntax known as array literal.

```
// Creating an array with initial items
// Creating an array with the constructor:
var foo = new Array;

// Creating an array with the array literal syntax:
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 possible 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.
There is an important distinction to be made between the two. Both an array construnctor and an array literal can contain items to be added to the array upon creating it. However, if just a single numeric item is passed in, the array constructor will assume its length to be that value.

```
// The array literal returns a bar.length value of 1:
var foo = [ 100 ];
alert( foo[0] );
alert( foo[0] ); // => 100
alert( foo.length );

// The array constructor returns a bar.length value of 100:
var bar = new Array( 100 );
alert( bar[0] );
alert( bar[0] ); // => undefined
alert( bar.length );
```

An array can be manipulated through the methods that are available 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.
An array can be manipulated through methods that are available on the instance of the array. Items in the array can be accessed using bracket notation with a given. If the index does not exist or contains no value, the return type will be undefined.

A few common array methods are shown below:

```
// Using the push(), pop(), unshift() and shift() methods.
// Using the push(), pop(), unshift() and shift() methods on an array
var foo = [];

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

alert( foo[0] );
alert( foo[1] );
alert( foo[0] ); // => a
alert( foo[1] ); // => b

alert( foo.length );
alert( foo.length ); // => 2

foo.pop();

alert( foo[0] );
alert( foo[1] );
alert( foo[0] ); // => a
alert( foo[1] ); // => undefined

alert( foo.length );
alert( foo.length ); // => 1

foo.unshift('z');

alert( foo[0] );
alert( foo[1] );
alert( foo[0] ); => z
alert( foo[1] ); => a

alert( foo.length );
alert( foo.length ); => 2

foo.shift();

alert( foo[0] );
alert( foo[1] );
alert( foo[0] ); // => a
alert( foo[1] ); // => undefined

alert( foo.length );
alert( foo.length ); // => 1
```

There are many more methods for manipulating arrays, details can be found on the [MDN Document](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array "MDN - Array Reference")
There are many more methods for manipulating arrays. Details can be found on the [Mozilla Developer Network](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array "MDN - Array Reference").

## Type Checking With jQuery
## Type Checking with jQuery

jQuery offers a few basic utility methods for determining the type of a
specific value.
Expand All @@ -183,7 +190,7 @@ specific value.
// Checking the type of an arbitrary value
var myValue = [ 1, 2, 3 ];

// Using JavaScript's typeof operator to test for primitive types
// Using JavaScript's typeof operator to test for primitive types:
typeof myValue == "string"; // false
typeof myValue == "number"; // false
typeof myValue == "undefined"; // false
Expand Down