You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/javascript-101/types.md
+40-36
Original file line number
Diff line number
Diff line change
@@ -16,47 +16,52 @@ The types in JavaScript fall into two categories; primitives and objects. The pr
16
16
17
17
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.
18
18
19
-
<javascriptcaption="Strings can created with double or single quotes.">
19
+
```js
20
+
// Strings can created with double or single quotes.
20
21
var a ="I am a string";
21
22
var b ='So am I!';
22
23
23
24
alert(a);
24
25
alert(b);
25
-
</javascript>
26
+
```
26
27
27
-
<javascriptcaption="Sometimes a string may contain quotation marks.">
28
+
```js
29
+
// Sometimes a string may contain quotation marks.
28
30
var statement1 ='He said "JavaScript is awesome!"';
29
31
var statement2 ="He said \"JavaScript is awesome!\"";
30
-
</javascript>
32
+
```
31
33
32
34
### Number
33
35
34
36
Number types are just any positive or negative numeric value, there is no distinction between integer and floating point values.
35
37
36
-
<javascriptcaption="Numbers are any whole or floating point integer.">
38
+
```js
39
+
// Numbers are any whole or floating point integer.
37
40
var num1 =100;
38
41
var num2 =100.10;
39
42
var num3 =0.10;
40
-
</javascript>
43
+
```
41
44
42
45
### Boolean
43
46
Boolean types are just simply true or false.
44
47
45
-
<javascriptcaption="Boolean values.">
48
+
```js
49
+
// Boolean values.
46
50
var okay =true;
47
51
var fail =false;
48
-
</javascript>
52
+
```
49
53
50
54
### Undefined and Null
51
55
52
56
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.
53
57
54
-
<javascriptcaption="Two ways to acheive an undefined value.">
58
+
```js
59
+
\\ Two ways to achieve an undefined value.
55
60
var foo =null;
56
61
57
62
var bar1 =undefined;
58
63
var bar2;
59
-
</javascript>
64
+
```
60
65
61
66
### Objects
62
67
@@ -66,9 +71,10 @@ Everything else is in JavaScript is considered an Object. While there are [numer
66
71
* Array
67
72
* Function
68
73
69
-
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".
74
+
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".
70
75
71
-
<javascriptcaption="Simple objects using the constructor or the literal syntax.">
76
+
```js
77
+
// Simple objects using the constructor or the literal syntax.
72
78
var person1 =newObject;
73
79
74
80
person1.firstName="John";
@@ -82,49 +88,53 @@ var person2 = {
82
88
};
83
89
84
90
alert(person2.firstName+""+person2.lastName);
85
-
</javascript>
91
+
```
86
92
87
-
<javascriptcaption="As mentioned, objects can also have objects as a property.">
93
+
```js
94
+
// As mentioned, objects can also have objects as a property.
88
95
var people = {};
89
96
90
97
people['person1'] = person1;
91
98
people['person2'] = person2;
92
99
93
100
alert(people['person1'].firstName);
94
101
alert(people['person2'].firstName);
95
-
</javascript>
102
+
```
96
103
97
104
What happens if a property is accessed which has not been *defined* yet? Well, it will be a type of undefined.
98
105
99
-
<javascriptcaption="Properties that have not been created are undefined.">
106
+
```js
107
+
// Properties that have not been created are undefined.
100
108
var person = { name:"John Doe" };
101
109
alert(person.email); // => undefined
102
-
</javascript>
110
+
```
103
111
104
112
### Array
105
113
106
114
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.
107
115
108
-
<javascriptcaption="Creating an array with initial items">
116
+
```js
117
+
// Creating an array with initial items
109
118
var foo =newArray;
110
119
var bar = [];
111
-
</javascript>
120
+
```
112
121
113
-
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.
122
+
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.
114
123
115
-
<javascriptcaption="">
124
+
```js
116
125
var foo = [100];
117
126
alert(foo[0]);
118
127
alert(foo.length);
119
128
120
129
var bar =newArray(100);
121
130
alert(bar[0]);
122
131
alert(bar.length);
123
-
</javascript>
132
+
```
124
133
125
-
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.
134
+
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.
126
135
127
-
<javascriptcaption="Using the push(), pop(), unshift() and shift() methods.">
136
+
```js
137
+
// Using the push(), pop(), unshift() and shift() methods.
128
138
var foo = [];
129
139
130
140
foo.push('a');
@@ -155,7 +165,7 @@ alert(foo[0]);
155
165
alert(foo[1]);
156
166
157
167
alert(foo.length);
158
-
</javascript>
168
+
```
159
169
160
170
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")
161
171
@@ -164,10 +174,11 @@ There are many more methods for manipulating arrays, details can be found on the
164
174
jQuery offers a few basic utility methods for determining the type of a
165
175
specific value.
166
176
167
-
<javascriptcaption="Checking the type of an arbitrary value">
177
+
```js
178
+
// Checking the type of an arbitrary value
168
179
var myValue = [1, 2, 3];
169
180
170
-
// Using JavaScript's typeof operator to test for primative types
181
+
// Using JavaScript's typeof operator to test for primitive types
0 commit comments