Skip to content

JavaScript Data Types

Introduction to JavaScript Data Types

JavaScript, a dynamic language, offers various data types to handle different kinds of data efficiently. Understanding these types is crucial for effective programming in JavaScript.

Primitive Data Types

  • Numbers: Integer or floating-point values. JavaScript handles various operations like addition, subtraction, and more.
  • String: Represents textual data. It can be enclosed in double, single, or backticks for added functionality like embedding expressions.
  • Boolean: This type holds two values: true or false, often used in logical operations.
  • Undefined: Indicates variables that have been declared but not assigned a value.
  • Null: Represents the intentional absence of an object value.
  • BigInt: Handles large integers beyond the Number type's safe limit.
  • Symbol: Ensures unique identifiers, mainly used as keys for object properties.

Non-Primitive Data Type

  • Object: A collection of properties, used to store complex data structures.

Working with Primitive Data Types in JavaScript

JavaScript's flexibility with data types can be harnessed effectively with a proper understanding of each type. Here, we delve into each primitive data type with examples.

To verify the type of a value, JavaScript provides the typeof operator. It returns a string indicating the type, such as "number", "string", or "boolean". Note that typeof null returns "object", which is a long-standing language quirk. It also returns "function" for functions, which are technically objects but categorized separately.

Number: The Backbone of Mathematical Operations


javascript
let age = 25; // Integer
let price = 99.99; // Floating-point
console.log(typeof age); // "number"

Numbers in JavaScript are versatile and can represent integer, floating-point values, and special values like Infinity and NaN (Not a Number).

String: More Than Just Text


javascript
let greeting = "Hello, world!";
let response = `The time is ${new Date().getHours()}`;
console.log(typeof greeting); // "string"

Strings in JavaScript are immutable and can be manipulated using various methods to achieve desired outcomes.

Boolean: The Binary Decision Maker


javascript
let isAvailable = true;
let isOverAge = (age > 18);
console.log(typeof isAvailable); // "boolean"

Booleans are straightforward in JavaScript, primarily used in conditional statements and loops.

Undefined and Null: The Absence of Value


javascript
let uninitializedVar;
let emptyValue = null;
console.log(typeof uninitializedVar); // "undefined"
console.log(typeof emptyValue); // "object" (known quirk)

undefined and null are distinct in JavaScript, with undefined typically indicating an uninitialized variable and null used for intentionally empty values.

BigInt: Handling Large Numbers


javascript
const largeNumber = 9007199254740991n;
console.log(typeof largeNumber); // "bigint"

BigInt, a newer addition to JavaScript, allows the handling of integers larger than what the Number type can safely represent.

Symbol: Ensuring Uniqueness


Output appears here after Run.

Symbols create unique identifiers, essential in complex data structures where uniqueness is paramount.

Objects: The Building Blocks of Complex Structures

In JavaScript, objects are king. They allow you to store collections of data and more complex entities.


javascript
let person = {
  name: "John",
  age: 30,
  isStudent: false
};
console.log(typeof person); // "object"

Objects in JavaScript are mutable and can store various data types, including other objects, making them ideal for real-world data modeling.

Mastering Data Types for Effective JavaScript Programming

JavaScript also supports type coercion, where values are automatically converted from one type to another during operations. For explicit conversion, you can use built-in functions like Number(), String(), or Boolean().

Understanding and effectively using JavaScript's data types is fundamental to mastering the language. This comprehensive overview provides the groundwork for you to start manipulating these types in real-world scenarios confidently.

Practice

Which of the following are valid data types in JavaScript according to the content on w3docs.com?

Dual-run preview — compare with live Symfony routes.