Data Types

Understanding JavaScript data types is essential for working effectively in JavaScript. In this lesson, we’ll introduce you to JavaScript's different data types, how they’re used, and why they matter.

What are JavaScript Data Types?

JavaScript data types are divided into two main categories: Primitive Data Types and Non-Primitive (Reference) Data Type. There are eight data types in JavaScript: seven primitive types and one reference type.

Together, these categories make up JavaScript’s eight data types: seven primitives and one reference type.

Primitive Data Types: These are the building blocks of data.

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

Non-Primitive (Reference) Data Type: Primarily used for organizing and structuring data.

  • Object

The typeof Operator

To check the data type of any variable or value, you can use the typeof operator. This built-in JavaScript function returns the data type as a string, like string or number. Here’s how it’s used:

javascript
          typeof 'Hello World'; // without parentheses
typeof('Hello World'); // with parentheses
        

Both styles return string for a text value. Let’s explore each data type in more detail.

Primitive Data Types

Primitive types are simple data values stored directly in the memory, like numbers or text. Each time you use a primitive value, it creates a distinct copy in memory, so they’re independent of one another.

JavaScript's primitive types include String, Number, BigInt, Boolean, Undefined, Null, and Symbol.

String

A string represents text and is created by enclosing characters in:

  • Double quotes: "Hello"
  • Single quotes: 'Hello'
  • Backticks: Hello
javascript
          console.log(typeof 'Welcome to JSBites'); // 'string'
        

Strings are commonly used to display messages, store user input, and handle text-based data.

Number

The Number type represents both whole numbers and decimals (floating-point numbers) and can be positive, negative, or zero. Numbers are written without quotes.

javascript
          console.log(typeof 100); // 'number'
console.log(typeof -45.8); // 'number'
        

Note: Writing a number in quotes makes it a string:

javascript
          console.log(typeof '100'); // 'string'
        

BigInt

BigInt is used for working with numbers larger than JavaScript’s maximum safe integer (2^53 - 1). To create a BigInt, add an n to the end of a number.

javascript
          const bigNumber = 9007199254740991n;
console.log(typeof bigNumber); // 'bigint'
        

This type is useful for precise calculations with large numbers, like those needed in scientific computations.

Boolean

A Boolean holds one of two values: true or false. Booleans are commonly used in conditional logic to determine program flow.

javascript
          console.log(typeof true); // 'boolean'
console.log(typeof false); // 'boolean'
        

Comparing values also returns a Boolean result:

javascript
          console.log(5 > 3); // true
console.log(5 < 3); // false
        

Note: Wrapping true or false in quotes converts it to a string.

Undefined

When a variable is undefined, it means it has been declared but has not been assigned a value yet. This is the default state for unassigned variables.

javascript
          let name;
console.log(typeof name); // 'undefined'
        

Undefined is often used to represent the absence of data, as opposed to null, which explicitly means “no value.”

Null

The null value represents the intentional absence of any value. When a variable is set to null, it explicitly signifies “no value.”

javascript
          let emptyValue = null;
nconsole.log(typeof emptyValue); // 'object' (this is a historical quirk)
        

Although typeof null returns 'object', null itself is still a distinct primitive type.

Symbol

Symbol is a unique and immutable data type introduced in ES6. Each symbol is unique and is commonly used as a key for object properties, ensuring that no other property can have the same name.

javascript
          const uniqueID = Symbol('id');
console.log(typeof uniqueID); // 'symbol'
        

Symbols are especially useful in advanced scenarios for ensuring unique keys.

Non-Primitive (Reference) Data Type

Object

In JavaScript, the object is the only reference type among the eight data types. Objects are collections of properties, where each property has a key and a value.

An object is a complex structure that can store multiple values in key-value pairs, making it ideal for organizing data.

javascript
          const person = {
  name: 'Khaled',
  age: 30,
  eyeColor: 'brown'
};
console.log(typeof person); // 'object'
        

In this example, person is an object with properties name, age, and eyeColor. Objects are central to Object-Oriented Programming (OOP), which structures code into reusable components known as objects.

or a more in-depth look at OOP and its applications, check out our blog post, Functional Programming vs. Object-Oriented Programming in JavaScript.

Conclusion

we covered the eight fundamental data types in JavaScript: Primitive typesString, Number, BigInt, Boolean, Undefined, Null, and Symbol. Reference typeObject. Understanding these types is key to writing effective and efficient JavaScript code. We also introduced the typeof operator, which helps identify data types. With these concepts, you're better equipped to handle data and build dynamic applications.