Comp 110 Primitive Data Types

Simple Data Types

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.

number

The number type is used for numerical data, which includes positive, negative, decimal and whole numbers.

Examples of number literals:

110
-123.45

string

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.

Two strings (or a string and a number) can also be concatenated to be one string!

Examples of string literals:

"hello"
"COMP110"
"110"
"*Go heels!*"
"I am " + 10 + " years old";

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

For example:

let prestigiousAward = "Dundees";

// we could find the 'd' in Dundees by using 
prestigiousAward[3];

// if we wanted to find the length, we could use
prestigiousAward.length;

// this would evaluate to 7 

boolean

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:

true
false

More examples:

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