Types
Types in JavaScript fall into two categories: primitives or objects. Primitive types include:
- String
- Number
- Boolean
- Null
- Undefined
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.
1
2
3
4
5
6
7
|
|
1
2
3
4
|
|
Number
Number types are any positive or negative numeric value. There is no distinction between integer and floating point values.
1
2
3
4
|
|
Boolean
Boolean types are either true or false.
1
2
3
|
|
Null and Undefined
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.
1
2
3
4
5
|
|
Objects
Everything else in JavaScript is considered an Object. While there are numerous built-in objects, this chapter will cover:
- Object
- Array
- Function
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."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
|
1
2
3
4
5
6
7
8
|
|
If a property is accessed that has not been defined, it will return a type of undefined.
1
2
3
4
|
|
Objects are covered further in the Objects section.
Array
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.
1
2
3
4
5
|
|
There is an important distinction to be made between the two. Both an array constructor 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
|
There are many more methods for manipulating arrays, some of which are covered further in the Arrays section. Details can be found on the Mozilla Developer Network.
Type Checking with jQuery
jQuery offers a few basic utility methods for determining the type of a specific value. Type checking is covered further in the Testing Type section, but here are some examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
|