Dynamically-typed languages such as JavaScript, Python, and Ruby allow variables to be assigned different types of data during runtime (as the program executes).
For example, in JavaScript we can do this:
// Declare variable named data and initialize with a number
let data = 123
// Reassigning a different type of data is allowed
data = 'Hello'
However, statically-typed languages such as Java and C++ required a variable's type to be declared and cannot be changed:
// Declare a variable as an integer and initialize
int data = 123
// Assigning anything other than an integer raises an error
data = "Hello" // NOT ALLOWED
JavaScript has seven main data types:
- Six primitive data types which represent a single value
- and an Object (reference) type that can contain any number of primitive values and/or other objects.
Note that an object can be one of JavaScript's built-in object sub-types such as:
- Array
- Function (yes, functions are objects in JavaScript!)
- RegExp
- Date
- Error
- BigInt
- Boolean
- Null
- Undefined
- Number
- String
- Symbol
- Array
- Object
- Function
- RegEx
- Date
A string represents textual data with zero or more characters wrapped by single or double quotation marks such as "John"
or 'Jane'
. A pair of quotes with nothing between them is still a string - an empty string.
let myString = 'Hello World'
let myOtherString = "Hello World"
console.log(typeof myString)
Note that the typeof
operator itself always returns a string describing the data type.
A number represents a numeric value.
Unlike many other programming languages, there is no distinction between integer (15
, 3
, etc.) and floating-point/decimal types (17.24
, 3.1416
, etc.).
Internally, JS represents all numbers as floating-point values.
let myNumber = 15
console.log(typeof myNumber)
❓What will the following code print to the console?
console.log(typeof 12.34)
Whereas strings and numbers can have a virtually unlimited number of different values, the boolean data type only has two possible values: true
and false
.
Strings, numbers, and booleans are the most common data types used to "model" real-world data in applications.
For example, in a multiplayer gaming app, we would represent a player's Gamertag using a string.
Before moving on to review the other data types, let's brainstorm a couple of examples for each of the three common data types that would be used to represent information in that gaming app:
The null data type has only one value: null
. null is the value of a variable that explicitly has no value. Thus, it represents a lack of value. However...
console.log(typeof null)
< "object" // Fail! Remember, JS was written in 10 days by one dude!
Because of this behavior, the use of null is somewhat controversial in the JavaScript community.
A variable that has not been assigned a value is of type undefined
. For example:
let cohort // cohort currently holds undefined
console.log(cohort)
In addition, a function by default returns undefined
if a value was not explicitly returned using the return
keyword.
Lastly, you will see undefined
if you enter code directly into the browser’s console and it evaluates a statement that does not return a value.
console.log(typeof undefined)
console.log('hello')
The symbol data type was added with ES2015 and is primarily used to create unique and less visible properties on objects.
Their use is rare in general JavaScript programming.
The six data types we've looked at thus far are classified as primitive/value data types because they hold only a single value.
Most programming languages also have complex/reference data types designed to hold more complex data structures.
JavaScript only has one reference type - the object.
Objects are collections of zero or more key:value pairs known as properties.
We will learn more about objects in a later lesson.
For now, let's just verify what typeof
returns:
// We'll try an object
let myObject = {course: 'SEI', cohort: 4} // Object
console.log(typeof myObject)
// An array
let myArray = []
console.log(typeof myArray)
// And a Regular Expression
let myRegEx = /./
console.log(typeof myRegEx)
Although functions are also considered objects (callable objects to be exact), the typeof
operator returns a more helpful data type:
let myFunction = function(){}
console.log(typeof myFunction)
- Do all variables have a data type?
- Is
let _save = ''
a valid statement? - If a variable is not a string, number, boolean, null, undefined, or a symbol, it must be an __________.
JavaScript is very relaxed when it comes to data types. Contrary to non-dynamic languages, a variable can change its type.
let m = 15 // I'm a number
m = 'hey' // Now I'm a string!
JavaScript is friendly and tries to help us whenever it can. However, we all know that sometimes it's better to be left alone.
Try adding a string to a number. What did JS do?
Now try comparing a number and a string containing the same digits using the equality (==
) comparison operator
13 == "13" // returns true!
This is why, unless there's a reason not to do so, we use the strict equality operator (===
) as it will not perform type conversion.
We can easily convert a number to a string using the toString()
and toFixed()
methods:
let n = 123.456
let s1 = n.toString() // "123.456"
let s2 = n.toFixed(2) // "123.46"
There are a couple of handy methods used to convert strings to numbers: parseInt()
and parseFloat()
let s = "1234.567"
let n1 = parseInt(s) // 1234
let n2 = parseFloat(s) // 1234.456
Remember, however, that the data type for both flavors, integer, and float (short for floating-point), is number.