Data Types

Learn about Javascript Data Types, including primitive and reference types, type conversion, and type checking.

What is a JavaScript Data Type?Link to this section

A data type in JavaScript tells the computer what kind of value a variable is holding, such as a number, text, or true/false. Without data types, the computer would not know whether to add two values together like numbers or join them together like words.

This example shows a variable holding a number and using the typeof operator to check its data type.

  • `let` is the variable declaration keyword
  • `age` is the identifier or variable name
  • `25` is the numeric value stored in the variable
  • `typeof` is the operator used to find the data type

Data types are used everywhere in programming to ensure that values are processed correctly by mathematical and logical operations.

Why Do We Need JavaScript Data Types?Link to this section

Data types are necessary because different types of data require different operations and memory handling in the computer.

  • Store student marks as numbers to calculate the final average
  • Store customer names as text to print receipts
  • Store login status as a boolean to show or hide the dashboard
  • Store shopping cart items in an array to calculate the total bill
  • Store user profile details in an object for easy access
  • Represent missing or unassigned profile pictures using null or undefined

Treating price and quantity as numbers allows us to perform correct mathematical multiplication.

Because price and quantity are stored as number data types, JavaScript can multiply them correctly instead of treating them as text.

About JavaScript Data TypesLink to this section

JavaScript data types are broadly divided into two main categories: primitive types and reference types. Primitive types hold a single simple value, while reference types can hold collections of values or more complex entities. JavaScript is also dynamically typed, meaning a variable can hold a number at one moment and text at another.

CategoryTypes IncludedMutability
PrimitiveString, Number, Boolean, Undefined, Null, Symbol, BigIntImmutable
ReferenceObject, Array, FunctionMutable

A variable can change its data type freely during the execution of a program.

Types of JavaScript Data TypesLink to this section

1. String Data TypeLink to this section

The string data type represents textual data and is enclosed in single quotes, double quotes, or backticks. It is used for names, addresses, messages, and any other text.

A basic string storing a person's name.

Using template literals with backticks to insert variables directly into text.

tip

Use backticks for template literals when you need to embed variables inside strings easily.

warning

Do not mix single and double quotes for the same string enclosure, as it will cause a syntax error.

2. Number Data TypeLink to this section

The number data type represents both integer and floating-point numbers. JavaScript does not have separate types for integers and decimals.

Storing a decimal number representing money.

Calculating total price including tax using numbers.

note

JavaScript numbers follow the IEEE 754 standard, which can sometimes lead to small precision errors with decimals like 0.1 + 0.2.

3. Boolean Data TypeLink to this section

The boolean data type represents a logical entity with only two possible values: true or false. It is commonly used in conditional statements to make decisions.

A boolean representing user login state.

Evaluating a condition produces a boolean result.

tip

Name your boolean variables with prefixes like is, has, or can to make your code easier to read.

4. Undefined Data TypeLink to this section

A variable that has been declared but has not yet been assigned a value has the value undefined. It represents the total absence of a value.

An unassigned variable automatically holds the undefined value.

warning

Avoid assigning undefined manually to a variable; use null instead to show intentional absence of value.

5. Null Data TypeLink to this section

The null data type represents an intentional absence of any object value. It is often assigned to a variable to clear its current value or show that something does not exist.

Explicitly setting a variable to have no value.

note

Due to a historical bug in JavaScript, typeof null returns object, even though null is a primitive type.

6. BigInt Data TypeLink to this section

BigInt is used to represent integers that are larger than the maximum safe integer limit for standard numbers in JavaScript.

Adding an n suffix to a number turns it into a BigInt.

warning

You cannot mix BigInt and standard number types in direct mathematical operations without explicit conversion.

7. Symbol Data TypeLink to this section

Symbols are unique and immutable primitive values that can be used as identifiers for object properties to prevent naming collisions.

Creating a unique symbol identifier.

note

Every symbol created with Symbol() is completely unique, even if they share the same description string.

8. Object Data TypeLink to this section

Objects are reference data types used to store collections of key-value pairs representing complex entities like users, products, or settings.

An object storing multiple related properties.

Using object properties in a practical e-commerce context.

tip

Use dot notation (object.property) or bracket notation (object["property"]) to access object values.

9. Array Data TypeLink to this section

Arrays are a special kind of object used to store ordered lists of values, such as lists of names, numbers, or other objects.

An array stores items in an indexed list starting at zero.

note

Arrays in JavaScript are zero-indexed, meaning the first item is at index 0.

Type Conversion and CoercionLink to this section

JavaScript often converts data types automatically in a process called type coercion, but developers can also convert types manually using functions like Number(), String(), and Boolean().

JavaScript automatically converts the string "5" into a number during subtraction.

Manual conversion of a string to a number.

warning

Using the plus operator (+) with strings will cause string concatenation instead of addition.

Complete Coding ExampleLink to this section

This complete program demonstrates strings, numbers, booleans, arrays, and objects working together in a student profile.

This script pulls together different data types to represent a student's academic record and personal profile.

Important NotesLink to this section

  • JavaScript has primitive and reference data types.
  • Primitive types include String, Number, Boolean, Undefined, Null, BigInt, and Symbol.
  • Reference types include Object, Array, and Function.
  • Variables in JavaScript are dynamically typed and can hold any data type.
  • The typeof operator is used to check the data type of a variable.
  • An unassigned variable always has the value undefined.
  • Null represents the intentional absence of any object value.
  • Type coercion happens when JavaScript automatically converts data types during operations.
  • Strings can be enclosed in single quotes, double quotes, or backticks.
  • Arrays are ordered lists of values stored with zero-based indexing.

SummaryLink to this section

In this lesson, you learned about JavaScript data types, which define what kind of value a variable holds. We covered primitive types like numbers, strings, and booleans, as well as reference types like objects and arrays. Understanding data types is essential for writing error-free code and performing correct calculations.

Check Your UnderstandingLink to this section

Javascript Data Types quiz
Question 1 of 6

Which operator is used to check the data type of a variable?

Practice ChallengesLink to this section

1. Store User Bio

easy

Create variables for a user's name (string), age (number), and subscription status (boolean). Print each variable using console.log.

Expected output

John
25
true

Hint: Use let to declare the variables and console.log to output each one.

2. Check Types

easy

Create three variables holding a string, a number, and a boolean. Use typeof to print the data type of each variable.

Expected output

string
number
boolean

Hint: Combine typeof with console.log.

3. Shopping Receipt

medium

Create an object representing a product with properties title, price, and inStock. Print a formatted message using these properties.

Expected output

Product: Shoes, Price: 49.99, In Stock: true

Hint: Use dot notation to access object properties inside a template literal.

4. Array Items

medium

Create an array of three favorite food items and print the second item in the array.

Expected output

Pizza

Hint: Remember that array indexes start at 0.

5. Type Coercion Check

hard

Perform subtraction between a string containing a number and a regular number, then print the result and its data type.

Expected output

5
number

Hint: Subtraction coerces the string into a number automatically.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion