Data Types
Data types are used to classify eight fundamental values used for programming in JavaScript, including primitive and object types.
Primitive Data Types
JavaScript uses seven primitive data types that are listed in the table below:
Data Type | Wrapper Object | Description |
---|---|---|
number |
Number |
Any number, including numbers with decimals: 1 , -2 , 99 , 3.14 . |
bigint |
BigInt |
Any number, greater than 253-1 or less than -(253-1) with n appended to the number: 1234567890123456n . |
string |
String |
Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single '' or double "" . |
boolean |
Boolean |
This data type only has two possible values — either true or false . |
null |
None | This data type represents the intentional absence of a value, and is represented by the keyword null . |
undefined |
None | This data type is denoted by the keyword undefined . It also represents the absence of a value though it has a different use than null . |
symbol |
Symbol |
A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now. |
Objects
Objects are a little more complex since they are collections of related data.
Note: The
null
type cannot be tested withtypeof
like all the other primitive types because it will return"object"
. This must be tested with the type equality operator (i.e.,=== null
).
Number and BigInt
In JavaScript, numbers are always stored as double-precision floating point numbers.
let num = 7;let bigNum = 9999999999999999n;
BigInt
is necessary for large whole numbers because they are unreliable with the Number
type:
console.log(9999999999999999);console.log(9999999999999999n);
This will output the following:
100000000000000009999999999999999n
The integer precision for the Number
type is 15 digits
. The following example is runnable:
String
Strings in JavaScript can be defined with either ' '
or " "
.
let greeting = 'Hi buddy';let message = 'You are doing great! Keep studying!';
To find the length of the string, the built-in length
property is used. Run the following example to get a better idea:
Boolean
Boolean is for truthy or falsy values:
let lateToWork = true;let isTheEarthFlat = false;
To convert a non-boolean value into a boolean, the Boolean
function or double not !!
is used. Run the following code block to see how Boolean()
works:
Null and Undefined
Null and Undefined are both for the absence of a value, but they have different meanings.
// Undefined means there should be some values, but it is undefined nowlet finishCourseTime = undefined;// Null means there is no value herelet finishStudyingDate = null;
A function also outputs undefined
when no value is returned.
The same function outputs null
when null is returned.
Object
Objects can have fields with different values and types inside:
const user = {name: 'Jane',age: 20,isActive: true,};
Alternatively, an empty object
can be initialized and the properties added to the object. Run the code below to see it in action:
All contributors
- Anonymous contributor
- Anonymous contributor
- Anonymous contributor
- christian.dinh
- goku-kun
- THE-Spellchecker
- Anonymous contributor
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.