Skip to content

Commit f2a06c3

Browse files
author
Garrett Johnson
committed
new section on types, functions will get their own section
1 parent ea1d637 commit f2a06c3

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

content/javascript-101/types.md

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
chapter: javascript-101
3+
section: 2
4+
title: Types
5+
attribution: jQuery Fundamentals
6+
github: jquery
7+
---
8+
9+
The types in JavaScript fall into two categories; primitives and objects. The primitive types include:
10+
11+
* string
12+
* number
13+
* boolean
14+
* null
15+
* undefined
16+
17+
### String
18+
19+
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.
20+
21+
<javascript caption="Strings can created with double or single quotes.">
22+
var a = "I am a string";
23+
var b = 'So am I!';
24+
25+
alert(a);
26+
alert(b);
27+
</javascript>
28+
29+
<javascript caption="Sometimes a string may contain quotation marks.">
30+
var statement1 = 'He said "JavaScript is awesome!"';
31+
var statement2 = "He said \"JavaScript is awesome!\"";
32+
</javascript>
33+
34+
### Number
35+
36+
Number types are just any positive or negative numeric value, there is no distinction between integer and floating point values.
37+
38+
<javascript caption="Numbers are any whole or floating point integer.">
39+
var num1 = 100;
40+
var num2 = 100.10;
41+
var num3 = 0.10;
42+
</javascript>
43+
44+
### Boolean
45+
Boolean types are just simply true or false.
46+
47+
<javascript caption="Boolean values.">
48+
var okay = true;
49+
var fail = false;
50+
</javascript>
51+
52+
### Undefined and Null
53+
54+
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.
55+
56+
<javascript caption="Two ways to acheive an undefined value.">
57+
var foo = null;
58+
59+
var bar1 = undefined;
60+
var bar2;
61+
</javascript>
62+
63+
### Objects
64+
65+
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:
66+
67+
* Object
68+
* Array
69+
* Function
70+
71+
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".
72+
73+
<javascript caption="Simple objects using the constructor or the literal syntax.">
74+
var person1 = new Object;
75+
76+
person1.firstName = "John";
77+
person1.lastName = "Doe";
78+
79+
alert(person1.firstName + " " + person1.lastName);
80+
81+
var person2 = {
82+
firstName: "Jane",
83+
lastName: "Doe"
84+
};
85+
86+
alert(person2.firstName + " " + person2.lastName);
87+
</javascript>
88+
89+
<javascript caption="As mentioned, objects can also have objects as a property.">
90+
var people = {};
91+
92+
people['person1'] = person1;
93+
people['person2'] = person2;
94+
95+
alert(people['person1'].firstName);
96+
alert(people['person2'].firstName);
97+
</javascript>
98+
99+
What happens if a property is accessed which has not been *defined* yet? Well, it will be a type of undefined.
100+
101+
<javascript caption="Properties that have not been created are undefined.">
102+
var person = { name: "John Doe" };
103+
alert(person.email); // => undefined
104+
</javascript>
105+
106+
### Array
107+
108+
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.
109+
110+
<javascript caption="Creating an array with initial items">
111+
var foo = new Array;
112+
var bar = [];
113+
</javascript>
114+
115+
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.
116+
117+
<javascript caption="">
118+
var foo = [100];
119+
alert(foo[0]);
120+
alert(foo.length);
121+
122+
var bar = new Array(100);
123+
alert(bar[0]);
124+
alert(bar.length);
125+
</javascript>
126+
127+
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.
128+
129+
<javascript caption="Using the push(), pop(), unshift() and shift() methods.">
130+
var foo = [];
131+
132+
foo.push('a');
133+
foo.push('b');
134+
135+
alert(foo[0]);
136+
alert(foo[1]);
137+
138+
alert(foo.length);
139+
140+
foo.pop();
141+
142+
alert(foo[0]);
143+
alert(foo[1]);
144+
145+
alert(foo.length);
146+
147+
foo.unshift('z');
148+
149+
alert(foo[0]);
150+
alert(foo[1]);
151+
152+
alert(foo.length);
153+
154+
foo.shift();
155+
156+
alert(foo[0]);
157+
alert(foo[1]);
158+
159+
alert(foo.length);
160+
</javascript>
161+
162+
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")
163+
164+
165+
166+
167+
168+
169+
170+
171+

0 commit comments

Comments
 (0)