4.1. Values and Data Types¶
Programs may be thought of as being made up of two things:
Operations that manipulate data
This chapter focuses primarily on the first of these two fundamental components, data.
Data can be stored in a program in a variety of ways. The most basic unit of data is a value.
A value is a specific piece of data, such as a word or a number. Some examples are
Each value belongs to a category called a data type. We will see many
different data types throughout the course, the first two of which are the
number and string types. Numeric values such as
numbers. Sequences of characters enclosed in quotes, such as
"Hello, World!", are strings, so-called because they contain a string of
letters. Strings must be enclosed in either single or double quotes.
If you are not sure what data type a value falls into, precede the value with
1 2 3
console.log(typeof "Hello, World!"); console.log(typeof 17); console.log(typeof 3.14);
string number number
"Hello, World!" is
string, while the data type of both
console.log(typeof "Hello, World!"); prints out
string to the console. The
typeof keyword is not printed to the console because the statement
typeof "Hello, World!" is an expression. Briefly, expressions are code segments that are reduced to a value. We will learn more about expressions soon.
typeof does not require parentheses).
There are data types other than string and number, including object and function, which we will learn about in future chapters.
4.1.1. More On Strings¶
What about values like
"3.2"? They look like numbers, but they are in quotation marks like strings.
Run the following code to find out.
console.log(typeof "17"); console.log(typeof "3.2");
What is the data type of the values
') or double
console.log(typeof 'This is a string'); console.log(typeof "And so is this");
Double-quoted strings can contain single quotes inside them, as in
"Bruce's beard", and single quoted strings can have double quotes inside them, as in
'The knights who say "Ni!"'.
If a string contains a single quote (such as
"Bruce's beard") then surrounding it with single quotes gives unexpected results.
4.1.2. More On Numbers¶
When you type a large integer value, you might be tempted to use commas between groups of three digits, as in
42000 42 0
42,000 as a pair of values. In fact, the
console.log function can print any number of values as long as you separate them by commas. Notice that the values are separated by spaces when they are displayed.
console.log(42, 17, 56, 34, 11, 4.35, 32); console.log(3.4, "hello", 45);
42 17 56 34 11 4.35 32 3.4 hello 45
Remember not to put commas or spaces in your integers, no matter how big they are. Also revisit what we said in the chapter How Programs Work: formal languages are strict, the notation is concise, and even the smallest change might mean something quite different from what you intend.
4.1.3. Type Systems¶
Every programming language has a type system, which is the set of rules that determine how the languages deals with data of different types. In particular, how values are divided up into different data types is one characteristic of a type system.
In many programming languages, integers and floats are considered to be different data types. For example, in Python
42 is of the
int data type, while
42.0 is of the
float data type.
When discussing the differences between programming languages, the details of type systems are one of the main factors that programmers consider. There are other aspects of type systems beyond just how values are categorized. We will explore these in future lessons.
4.1.4. Check Your Understanding¶