This document is intended to be an introduction to the data types that are utilized in JavaScript.
- Numbers
- Integers are whole numbers, without decimals or fractional parts of numbers. (ex. 42, 78)
- Floats have a decimal and fractional parts. (ex. 1.23, 42.42)
- Strings
Strings must be inside quotes. Whether you use single or double quotes is up to you (and your team). Even numbers are strings, if they're inside quotes. (ex.
"hello world"
) - Boolean
A boolean is a variable that must resolve to true or false. (ex.
var isToggled = true
) - Arrays
A list. Arrays must be inside square brackets. The items inside an array can be any data type (string, number, more arrays, objects). (ex.
[2, "hello", 4, "world", true]
) - Objects
A list of key/value pairs that are related (the relationship is important here, because the key refers to the value). An object is more dimensional than a plain old array, as it has pairs that are explicitly described. The key can be a number or a string, and the value can be any of the previously mentioned data types, and they are separated by a colon. (ex.
var myObj = {myKey: 'myValue'}
)
var myNumber = 42;
var myString = "You're an angry little elf!!";
var myBoolean = true;
var myArray = ['candy', 'candy canes', 'candy corn', 'syrup'];
var myObject = {name: 'Buddy', species: 'Elf'}