Data types represent the type of data that is used in a programming language.
A string is a data type used to represent text.
Strings are inclusive of numbers, letters, spaces, and special characters.
Strings represent a piece of information that is constant.
Here are two examples of a string data type:
-
<p>"Turing is providing an amazing opportunity for prospective software engineers"</p>
-
"(123) 456 - 7890"
An integer is a whole number ranging from negative numbers to positive numbers, inclusive of 0.
Integers are not decimal numbers, nor are they fractions.
Here are two examples of an integer data type:
-
38
-
-600
A float is a decimal number that can be either negative or positive.
Here are two examples of a float data type:
-
-8.65
-
3.62
A boolean is a data type that has a value of either true or false, typically in response to a true/false scenario such as an if/else conditional statement.
When determining whether a statement is true or false, operators can be used within the statement that is being assessed.
- && is an operator for 'and'
- || is an operator for 'or'
- == is an operator for 'equal in value but not necessarily in data type'.
When it comes to the == operator, something like 3 == "three" would return true even though 3 is an integer and "three" is a string.
Using the === operator means the values on either side of the operator must match according to data type and value.
In this case, 3 === "three" would return false, while 3 === 3 and "three" === "three" would both return true.
-
If a statement includes the && operator, statements on both sides of the operator must be true in order to return a boolean value of true.
-
If a statement includes the || operator, at least one statement on either side of the operator must be true in order to return a boolean value of true.
Here are the only two values of a boolean:
-
true
-
false
An array is a data structure that represents a collection of similar items.
It is best practice to include a single data type as items in an array, rather than mixing two data types such as an integer and a string.
The syntax of an array includes [ ], where all items in the collection are written inside the brackets.
Here are two examples of an array data type:
- Including integers only:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- Including strings only:
["Santa", "Matt", "Nicole", "Rio", "Tom", "Harper"]
A hash or object is a collection of key-value pairs.
For this data type, items are paired together rather than listed as independent items in a collection, the way they are laid out in an array.
With a hash or object, two pieces of information are meant to have a purposeful relationship.
The syntax of a hash or object includes { }, or curly braces.
Here are two examples of an extension:
{"Ms. Gooden's Class": 26, "Ms. Trecek's Class": 27, "Mr. Bartko's Class": 25}
where the key represents the teacher's name and the value represents the number of students in each teacher's class.
{"Fifth Grade": "Ms. Trecek, Ms. Gooden, and Mr. Bartko", "Fourth Grade": "Ms. Franco, Ms. Farnell, and Ms. Barber"}
where the key represents the elementary grade level and the value represents the names of the teachers per each grade level.}
To conclude, there are 6 data types defined and explained in this gist. Each data type has a purpose and exists in Javascript code.