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
+27-28Lines changed: 27 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
Types in JavaScript fall into two categories: primitives or objects. Primitive types include:
9
+
Types in JavaScript fall into two categories: primitives and objects. Primitive types include:
10
10
11
11
* String
12
12
* Number
@@ -67,13 +67,13 @@ var bar2;
67
67
68
68
## Objects
69
69
70
-
Everything else 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:
70
+
Everything else 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:
71
71
72
72
* Object
73
73
* Array
74
74
* Function
75
75
76
-
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."
76
+
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."
77
77
78
78
```
79
79
// Creating an object with the constructor:
@@ -109,14 +109,14 @@ If a property is accessed that has not been defined, it will return a type of `u
109
109
```
110
110
// Properties that have not been created are undefined.
111
111
var person = { name: "John Doe" };
112
-
alert( person.email ); // => undefined
112
+
alert( person.email ); // undefined
113
113
```
114
114
115
115
Objects are covered further in the [Objects](/objects/) section.
116
116
117
117
### Array
118
118
119
-
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.
119
+
An array is a type of object that is 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`. Similar to a basic object, an array can be created with the `Array` constructor or the shorthand syntax known as array literal.
120
120
121
121
```
122
122
// Creating an array with the constructor:
@@ -131,51 +131,52 @@ There is an important distinction to be made between the two. Both an array cons
131
131
```
132
132
// The array literal returns a foo.length value of 1:
133
133
var foo = [ 100 ];
134
-
alert( foo[0] ); // => 100
135
-
alert( foo.length ); // => 1
134
+
alert( foo[ 0 ] ); // 100
135
+
alert( foo.length ); // 1
136
136
137
137
// The array constructor returns a bar.length value of 100:
138
138
var bar = new Array( 100 );
139
-
alert( bar[0] ); // => undefined
140
-
alert( bar.length ); // => 100
139
+
alert( bar[ 0 ] ); // undefined
140
+
alert( bar.length ); // 100
141
141
```
142
142
143
143
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 index. If the index does not exist or contains no value, the return type will be `undefined`.
144
144
145
145
A few common array methods are shown below:
146
146
147
147
```
148
-
// Using the push(), pop(), unshift() and shift() methods on an array
148
+
// Using the push(), pop(), unshift() and shift() methods on an array.
149
+
149
150
var foo = [];
150
151
151
152
foo.push( "a" );
152
153
foo.push( "b" );
153
154
154
-
alert( foo[ 0 ] ); // => a
155
-
alert( foo[ 1 ] ); // => b
155
+
alert( foo[ 0 ] ); // a
156
+
alert( foo[ 1 ] ); // b
156
157
157
-
alert( foo.length ); // => 2
158
+
alert( foo.length ); // 2
158
159
159
160
foo.pop();
160
161
161
-
alert( foo[ 0 ] ); // => a
162
-
alert( foo[ 1 ] ); // => undefined
162
+
alert( foo[ 0 ] ); // a
163
+
alert( foo[ 1 ] ); // undefined
163
164
164
-
alert( foo.length ); // => 1
165
+
alert( foo.length ); // 1
165
166
166
167
foo.unshift( "z" );
167
168
168
-
alert( foo[ 0 ] ); => z
169
-
alert( foo[ 1 ] ); => a
169
+
alert( foo[ 0 ] ); // z
170
+
alert( foo[ 1 ] ); // a
170
171
171
-
alert( foo.length ); => 2
172
+
alert( foo.length ); // 2
172
173
173
174
foo.shift();
174
175
175
-
alert( foo[ 0 ] ); // => a
176
-
alert( foo[ 1 ] ); // => undefined
176
+
alert( foo[ 0 ] ); // a
177
+
alert( foo[ 1 ] ); // undefined
177
178
178
-
alert( foo.length ); // => 1
179
+
alert( foo.length ); // 1
179
180
```
180
181
181
182
There are many more methods for manipulating arrays, some of which are covered further in the [Arrays](/arrays/) section. Details can be found on the [Mozilla Developer Network](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array"MDN - Array Reference").
@@ -185,22 +186,20 @@ There are many more methods for manipulating arrays, some of which are covered f
185
186
jQuery offers a few basic utility methods for determining the type of a specific value. Type checking is covered further in the [Testing Type](/testing-type/) section, but here are some examples:
186
187
187
188
```
188
-
// Checking the type of an arbitrary value
189
+
// Checking the type of an arbitrary value.
190
+
189
191
var myValue = [ 1, 2, 3 ];
190
192
191
193
// Using JavaScript's typeof operator to test for primitive types:
192
-
193
194
typeof myValue === "string"; // false
194
195
typeof myValue === "number"; // false
195
196
typeof myValue === "undefined"; // false
196
197
typeof myValue === "boolean"; // false
197
198
198
-
// Using strict equality operator to check for null
199
-
199
+
// Using strict equality operator to check for null:
200
200
myValue === null; // false
201
201
202
-
// Using jQuery's methods to check for non-primitive types
203
-
202
+
// Using jQuery's methods to check for non-primitive types:
0 commit comments