Essentials
Data Types
Learn about data types in JavaScript.
String
A text of characters enclosed in quotes.
const stringValue = 'Hello, world!';
const stringValue2 = "Hello, world!";
Using single quotes for strings is way more common than using double quotes.
Number
A number representing a mathematical value.
const numberValue = 10;
const numberValue2 = 10000;
Boolean
A data type representing true or false.
const booleanValue = true;
const booleanValue2 = false;
Array
Arrays are ordered lists of values.
const firstArray = [1, 2, 3];
const secondArray = [10, 'a string', { prop: 'value' }, [1, 2, 3]];
The index starts at 0. So we use firstArray[0] to indicate the first value of firstArray.
console.log(firstArray[0]);
1
console.log(secondArray[3]);
Array [ 1, 2, 3 ]
console.log(secondArray[3][0]);
1
Object
A collection of key-value pairs of data.
const objectVariable = { prop1: 20, prop2: 50 };
console.log(objectVariable);
Object { prop1: 20, prop2: 50 }
Objects can be infinitely nested. Unlike arrays, we use the dot notation to access the properties.
const nestedObject = {
layer1: {
layer2: {
layer3: {
targetValue: 20
}
}
}
};
console.log(nestedObject.layer1.layer2.layer3.targetValue);
20
We can also retrieve a value from an object by specifying the property.
console.log(objectVariable['prop2']);
50