DevAcademy
LearnJavaScriptJavaScript Data Types
BeginnerJavaScript

JavaScript Data Types

Learn about JavaScript data types, understand the difference between primitive and non-primitive types, and see how they are used in real-world applications.

Reading Time

12 min

Lesson

Lesson 5 of 48

What are Data Types?

A data type defines the kind of value a variable can store. JavaScript supports different types of data such as numbers, strings, booleans, objects, and more. Understanding data types is essential because every operation in JavaScript depends on the type of data being used.

Why are Data Types Important?

Data types help JavaScript determine what operations can be performed on a value. For example, numbers can be added, while strings can be concatenated. Choosing the correct data type makes your programs more reliable and easier to understand.

JavaScript Data Types

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • BigInt
  • Symbol
  • Object

Primitive vs Non-Primitive

PrimitiveNon-Primitive
StringObject
NumberArray
BooleanFunction
UndefinedDate
NullMap / Set
BigIntCustom Objects
Symbol-

1. String

A string is a sequence of characters enclosed in single quotes, double quotes, or backticks. Strings are commonly used to store names, messages, and text.

String Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

2. Number

The Number type represents both integers and decimal values. JavaScript does not have separate types for int and float.

Number Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

3. Boolean

A Boolean represents one of two values: true or false. It is mainly used in conditions and decision-making.

Boolean Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

4. Undefined

A variable that has been declared but not assigned a value has the value undefined.

Undefined Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

5. Null

Null represents the intentional absence of a value. It is assigned explicitly by the developer.

Null Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

6. BigInt

BigInt is used for very large integers that cannot be represented safely by the Number type.

BigInt Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

7. Symbol

A Symbol is a unique and immutable value. It is commonly used as object property keys to avoid conflicts.

Symbol Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

8. Object

Objects are collections of key-value pairs. Arrays, functions, and dates are all special types of objects in JavaScript.

Object Example

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

typeof Operator

ValueResult
"Hello""string"
100"number"
true"boolean"
undefined"undefined"
null"object" (historical bug)
{}"object"
[]"object"
Symbol()"symbol"

Using typeof

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Common Interview Question

Many developers expect typeof null to return "null". However, it returns "object". This is a well-known historical bug in JavaScript and is frequently asked in interviews.

Best Practice

Choose the correct data type for the data you want to store. Use null intentionally when a value is absent, and avoid relying only on typeof for complex type checking.

Summary

JavaScript provides seven primitive data types and one non-primitive type (Object). Understanding data types is essential because every variable, function, and expression in JavaScript works with them.

Interview Questions

Quick Quiz

1. Which of the following is NOT a primitive data type?

2. What is the output of typeof null?

3. Which data type is used to store very large integers?