Questions
What are the different data types in JavaScript?
The Scenario
You are a frontend engineer at a social media company. You are writing a new feature that needs to work with a variety of different data types.
The Challenge
Explain the different data types in JavaScript. What are the primitive data types, and what are the non-primitive data types?
A junior engineer might not be aware of all the different data types in JavaScript. They might also not be aware of the difference between primitive and non-primitive data types.
A senior engineer would be able to provide a detailed explanation of all the different data types in JavaScript. They would also be able to explain the difference between primitive and non-primitive data types.
Step 1: Understand the Primitive Data Types
| Data Type | Description |
|---|---|
string | A sequence of characters. |
number | A number, including integers and floating-point numbers. |
boolean | A boolean value, either true or false. |
null | A special value that represents the intentional absence of any object value. |
undefined | A special value that represents a variable that has been declared but has not yet been assigned a value. |
symbol | A unique and immutable value that can be used as the key of an object property. |
bigint | A number that can represent integers of arbitrary precision. |
Step 2: Understand the Non-Primitive Data Type
| Data Type | Description |
|---|---|
object | A collection of key-value pairs. |
Arrays and functions are also objects in JavaScript.
Step 3: The typeof Operator
You can use the typeof operator to check the type of a variable.
typeof "hello" // "string"
typeof 123 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" (this is a well-known bug in JavaScript)
typeof Symbol() // "symbol"
typeof 123n // "bigint"
typeof {} // "object" Practice Question
What will `typeof null` return?