🟨 JavaScript Q32 / 121

What are JavaScript data types?

AI-Powered Answer ✓ Answered

In JavaScript, data types classify the different kinds of values that variables can hold. Understanding data types is fundamental for writing effective and error-free code, as they dictate the operations that can be performed on a value.

Overview of JavaScript Data Types

JavaScript has a dynamic typing system, meaning variables are not explicitly declared with a specific type. Instead, their type is determined at runtime based on the value they hold. Data types in JavaScript are broadly categorized into two groups: Primitive (or Primitives) and Non-Primitive (or Objects).

Primitive Data Types

Primitive data types represent single, simple values. They are immutable, meaning their value cannot be changed after creation (though a variable can be reassigned to a new primitive value). JavaScript has 7 primitive data types:

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

String

Represents textual data. Strings are immutable sequences of characters enclosed in single quotes (''), double quotes (""), or backticks (``) for template literals.

javascript
let name = 'Alice';
let greeting = "Hello, " + name + "!";
let template = `Welcome, ${name}!`;

Number

Represents both integer and floating-point numbers. It can also represent special numeric values like Infinity, -Infinity, and NaN (Not-a-Number).

javascript
let age = 30;
let price = 19.99;
let result = 0 / 0; // NaN
let bigNum = 1e10; // 10000000000

BigInt

Introduced in ES2020, BigInt is a primitive data type that can represent whole numbers larger than 2^53 - 1 (the largest number that Number can reliably represent). A BigInt is created by appending n to an integer literal.

javascript
let bigNumber = 9007199254740991n; // A BigInt
let anotherBig = BigInt("12345678901234567890");

Boolean

Represents a logical entity and can have two values: true or false. Often used for conditional logic.

javascript
let isActive = true;
let hasPermission = false;

Undefined

Represents a variable that has been declared but not yet assigned a value. It's also the default return value for functions that don't explicitly return anything.

javascript
let myVariable;
console.log(myVariable); // undefined

Null

Represents the intentional absence of any object value. It's a primitive value often used to indicate 'no value' or 'nothing'.

javascript
let emptyValue = null;
console.log(emptyValue); // null

Symbol

Introduced in ES2015, Symbol is a unique and immutable primitive value that can be used as an identifier for object properties. Each Symbol value is unique, even if they have the same description.

javascript
const id = Symbol('id');
const anotherId = Symbol('id');
console.log(id === anotherId); // false

Non-Primitive Data Type: Object

Objects are complex data types that allow you to store collections of data and more complex entities. Unlike primitives, objects are mutable and are passed by reference. In JavaScript, almost everything else is an object or behaves like an object.

Object

The Object type is a collection of key-value pairs. Arrays and Functions are specialized types of objects.

javascript
let person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30
};

let colors = ['red', 'green', 'blue']; // Array (an object)

function greet(name) { // Function (an object)
  return `Hello, ${name}!`;
}

The typeof Operator

The typeof operator is used to determine the data type of a variable or an expression. It returns a string indicating the type.

javascript
console.log(typeof 'Hello');     // "string"
console.log(typeof 123);        // "number"
console.log(typeof 10n);        // "bigint"
console.log(typeof true);       // "boolean"
console.log(typeof undefined);  // "undefined"
console.log(typeof Symbol('id')); // "symbol"
console.log(typeof null);       // "object" (a historical bug in JavaScript)
console.log(typeof {});         // "object"
console.log(typeof []);         // "object"
console.log(typeof function(){}); // "function" (a special type of object)

Note: The typeof null returning "object" is a well-known historical quirk in JavaScript and is not indicative that null is an object. It's a primitive value.