Common JavaScript Interview Question: What are some data types in JavaScript?
There are 8 basic data types in JavaScript.
- Null
- Undefined
- Number
- Boolean
- BigInts
- Object
- String
- Symbol
How to remember them? Learn the mnemonic: NUNBBOSS.
Now that we know there are 8 data types, what exactly are these data types used for?
<script src=”https://gist.github.com/asingh714/b565f2361c804054343d11f72a67bf77.js"></script>
Of the 8 data types, 7 are primitive. And what exactly does “primitive” mean?
A primitive data type can contain only a single thing. All primitive types are immutable and we cannot set properties on them.
Objects are the only non-primitive type because they are used to store collections of data and more complex entities.
You might be thinking, “Hey but I heard that everything in JavaScript is an object! If a string is not an object, how come I can do the following:
<script src=”https://gist.github.com/asingh714/c909a321f3a8f76ac2f1dbb7e58fe3fa.js"></script>
The above logic makes the string “hi” look like an object because we are using a method on it. This is an illusion! JavaScript will create a temporary object when we want to use a method for a primitive type and then immediately discard it!