Numbers, strings, booleans, undefined, and null are 5 out of the 6 primitives, or basic data types. The other one is symbol, which represents an identifier for object properties. Don’t worry about symbol for now. A primitive data value is a single simple data value with no additional properties and methods.
The Concept of Data Types
In programming, data types is an important concept. To be able to operate on variables, it is important to know something about the type. Without primitives, a computer can not safely solve this:
let y = 22 + ' Daniel';
console.log(y); // '22 Daniel'
JavaScript Types are Dynamic
JavaScript has dynamic types. This means that the same variable can be used to hold different data types:
let x; // x is undefined
x = 5; // x is now a number
x = 'Dan'; // x is now a string
Data Types
Number
the number type represents numbers.
- 21
- 4.25
- 0
- -100
- NaN (means ‘not a number’, but is considered a number.)
- Infinity
- -Infinity
- let favoriteNumber = 9;
You can use certain methods on numbers. A method is a function which is a property of an object.
console.log(3.14159265359.toFixed(2)); // 3.14
String
the string type represents text. It is contained in ‘single’ or “double” quotes. You can use quotes inside a string, as long as they don’t match the quotes surrounding the string.
- ‘Hello, world!’
- ‘Brian said, “Hello There!”’
- “42”
- let greeting = ‘How are you doing?’;
You can use certain methods on strings.
console.log('Daniel'.toUpperCase()); // 'DANIEL'
console.log('Daniel'.charAt(2)); // 'n'
console.log('Daniel'.toUpperCase().charAt(2)); // 'N'
Boolean
The Boolean data type can only hold 2 possible values:
console.log(10 > 1); // true
console.log(1 > 10); // false
console.log('Daniel'.charAt(2) === 'a'); // false
let isNiceDay = true;
console.log(isNiceDay); // true
Undefined
When declaring a variable without giving it a value (let number;
), JavaScript creates the variable without a value, hence undefined
. Even when declaring a variable and assigning it a value at the same time (let favoriteNumber = 9;
), JavaScript actually creates the variable initially without a value, temporarily giving it a value of undefined
, before then assigning it the value to the right of the equals sign.
Additionally, there are some functions and methods that do not return any value, in which case the return value is actually undefined
.
Null
The null
data type is supposed to represent something that doesn’t exist. It’s data type is actually an object. You can consider it a bug, since it should be null
.
You can empty a variable by setting it to null:
let number = 22;
number = null // number is now null
console.log(number); // null
let person = {
firstName: 'Daniel',
age: 22,
eyeColor: 'black'
};
person = null; // person is now null
console.log(person); // null
Data Type Detection
It’s important to understand the difference between the number 5 and the string “5”. To a computer, they are two entirely different things, as shown in this example:
let number = 56;
let otherNumber = 2;
let string = '56';
let otherString = '2';
console.log(number + otherNumber); // 58
console.log(string + otherString); // '562'
When we added 56
to 2
we got 58
, but when we added '56'
to '2'
it concatenated, link (things) together in a chain or series, the two strings together.
Likewise, the boolean true
is not the same as the string 'true'
.
In the example above, the +
operator works on both numbers and strings, just differently. Usually methods will only work on a specific data type. For example, 3.14159.toFixed(2);
works just fine, but trying to do '3.14159'.toFixed(2);
results in an error because the toFixed
method doesn’t work on a string. Likewise, 'Hello'.charAt(2);
works, but 314159.charAt(2);
does not.
We can check the data type of a variable or value using typeof
as follows:
console.log(typeof 5); // 'number'
console.log(typeof '5'); // 'string'
console.log(typeof true); // 'boolean'
console.log(typeof 'true'); // 'string'
let greeting = 'How are you doing?';
console.log(typeof greeting); // 'string'
Data Type Conversion
Often input from a web browser will come in as a string and we will need to convert it to a number before working with it.
We can convert a string to a number by passing it to the Number()
function:
let inputAge = '22'; // inputAge is now a string
console.log(inputAge); // '22'
console.log(typeof inputAge); // 'string'
let myAge = Number(inputAge); // myAge is now a number
console.log(myAge); // 22
console.log(typeof myAge); // 'number'
Note that if you try to use the Number
function to convert a string not actually containing a number, the result is the number NaN
.
let name = 'Daniel';
let nameNumber = Number(name);
console.log(nameNumber); // NaN
If you need to go the other way around, you can convert a number to a string by calling the String
function on the number:
let number = 22;
console.log(number); // 22
console.log(typeof number); // 'number'
let stringNumber = String(number);
console.log(stringNumber); // '22'
console.log(typeof stringNumber); // 'string'
Conclusion
Numbers, strings, booleans, undefined, null, and symbol are the primitives, or basic data types. Without primitives, a computer can not safely solve problems.