There are three simple data types:
number, string, boolean
These data types are known as primitives. These store single pieces of data and are the building blocks of everything we will create in code. Primitive data types can be combined together to construct more complicated compound data types. On this page we'll give an overview of each primitive type.
The number type is used for numerical data, which includes positive, negative, decimal and whole numbers.
Examples of number literals:
The string type is used for textual data. All strings are enclosed in double quotes. Anything inside of these quotes is part of the string.
Examples of string literals:
"hello" "COMP110" "110" "*Go heels!*"
Note: "110" is treated as a string since it's in double quotes. This is NOT the same as the number 110.
However! Strings are also special because a string is actually an array of individual characters.
We don't actually have to worry about this when we're writing our code thanks to something called data abstraction, but it does let us do some interesting things with strings!
In order to access individual characters, we can do so just as we would in an array by using stringName[index]
If we wanted to access the length of the string we can use stringName.length
let prestigiousAward = "Dundees"; // we could find the 'd' in Dundees by using prestigiousAward; // if we wanted to find the length, we could use prestigiousAward.length; // this would evaluate to 7
The boolean type is used for true and false. A boolean can be either true or false - that's all folks!
Examples of boolean literals:
print("UNC"); // "UNC" is a string literal. This prints UNC print(2018); // 2018 is a number. This prints 2018 print(true); // true is a boolean. This prints true print("true"); // "true" is a string since it is in double quotes. This prints true print("1789"); // "1789" is a string since it's in double quotes. This prints 1789