JavaScript Data Types

JavaScript offers a variety of data types to handle different kinds of values. These include simple, easy-to-understand primitive types such as numbers, strings, and booleans, as well as more complex types like objects and arrays. Objects help you group related data together using key-value pairs, while arrays allow you to store lists of items in an ordered way. Understanding these fundamental data types is essential for writing clear, organized, and efficient JavaScript code.

Here's an overview of the most commonly used JavaScript data types:

Data TypeDescription
NumberRepresents both integers and floating-point numbers.
Ex: let age = 30;
StringRepresents textual data enclosed in quotes.
Ex: let name = "Tom";
BooleanRepresents either true or false.
Ex: let isOnline = true;
UndefinedA variable declared but not assigned a value.
Ex: let score;
NullRepresents an intentional absence of value.
Ex: let user = null;
BigIntRepresents very large integers beyond the limits of the Number type.
Ex: let largeNumber = 123456789012345678n;
SymbolA unique and immutable value often used as object property keys.
Ex: let id = Symbol('id');
ObjectA collection of key-value pairs used to store structured data. This is a non-primitive data type.
Ex: let person = { name: "John", age: 30 };
ArrayAn ordered list of values, accessed by index. Arrays are a special type of object in JavaScript.
Ex: let colors = ["red", "green", "blue"];


What You'll Learn

In this section, you will learn about the different data types available in Javascript. We'll discuss basic types like numbers, strings, and boolean values. By the end of this, you'll know how to use the right type for your data and how to manipulate it.


Understanding Data Types in JavaScript

JavaScript is also dynamically typed, which means you don't need to explicitly declare the type of a variable. The JavaScript engine automatically determines the data type based on the value assigned to the variable.

For example, if you assign a number to a variable, JavaScript will recognize it as a number type. Similarly, if you assign a string (text wrapped in quotes), JavaScript will treat it as a string. This flexibility makes JavaScript easy to work with, but also requires careful handling to avoid type-related errors.

Let’s dive deeper into each data type and how they work in Javascript.

1. Number (number)

A number in JavaScript represents both integers and floating-point values. JavaScript does not differentiate between integer numbers and decimal numbers—they are all treated as type number. You can perform various mathematical operations like addition, subtraction, multiplication, and division with numbers.

javascript
// Example of a number
let a = 5;
let b = 6;
console.log(a + b);

How It Works:

  • a: This variable stores the number value 5.
  • b: This variable stores the number value 6.
  • a + b: The expression adds the values of a and b, which are 5 and 6, respectively.
  • When the code runs, JavaScript calculates the sum of 5 and 6, resulting in 11.
  • The console.log() function displays the result, 11, in the console.

Output:

11

2. String (string)

A string in JavaScript represents a sequence of characters enclosed within single quotes (' '), double quotes (" "), or backticks (` `) for template literals. Strings are used to store and manipulate textual data. You can perform various string operations like concatenation, length calculation, and more.

javascript
// Example of a string
let greeting = "Hello, world!";
let name = "Tom";
console.log(greeting + " " + name);

How It Works:

  • greeting: This variable stores the string value "Hello, world!".
  • name: This variable stores the string value "Tom".
  • greeting + " " + name: This expression concatenates the two strings greeting and name with a space in between, producing the result "Hello, world! Tom".
  • When the code runs, JavaScript outputs the concatenated string, "Hello, world! Tom", in the console.
  • The console.log() function displays the result in the console.

Output:

Hello, world! Tom

3. Boolean (boolean)

A Boolean in JavaScript represents a logical value, which can either be true or false. It is often used in conditional statements to control the flow of code based on certain conditions.

javascript
// Example of a boolean
let isActive = true;
let isComplete = false;

console.log(isActive);  // Outputs: true
console.log(isComplete);  // Outputs: false

How It Works:

  • isActive: This variable stores the boolean value true, indicating that something is active or true.
  • isComplete: This variable stores the boolean value false, indicating that something is not complete or false.
  • When the code runs, JavaScript outputs the boolean values true and false to the console.
  • The console.log() function is used to display the values of isActive and isComplete.

Output:

true false

4. Undefined (undefined)

In JavaScript, a variable that has been declared but has not yet been assigned a value is of type undefined. This is the default value assigned to uninitialized variables. It signifies the absence of an assigned value but not the absence of the variable itself.

javascript
// Example of undefined
let score;
console.log(score);

How It Works:

  • score: This variable is declared but not assigned any value.
  • Since there is no assigned value, JavaScript automatically gives it the type undefined.
  • When you log score to the console, it shows undefined.

Output:

undefined

5. Null (null)

In JavaScript, null represents an intentional absence of any object value. Unlike undefined, which indicates a variable has been declared but not yet assigned a value, null is explicitly assigned to a variable as a representation of "no value."

javascript
// Example of null
let user = null;
console.log(user);

How It Works:

  • user: This variable is explicitly assigned the value null.
  • It means the variable exists, but currently holds no meaningful data or object.
  • When you log user to the console, it shows null, indicating the absence of a value.

Output:

null

6. Symbol (symbol)

The symbol data type in JavaScript is used to create unique and immutable values. Symbols are often used as unique identifiers for object properties, helping to avoid property name collisions, especially in large applications or libraries.

javascript
// Example of symbol
let id1 = Symbol('id');
let id2 = Symbol('id');

console.log(id1 === id2);

How It Works:

  • id1 and id2 are both symbols with the same description, 'id'.
  • Even though their descriptions are the same, each Symbol is unique.
  • The expression id1 === id2 returns false because no two symbols are equal, even if they have the same description.

Output:

false

Symbols are commonly used to define hidden object properties or to avoid naming conflicts when extending objects.


7. BigInt (bigint)

The BigInt data type allows you to represent integers larger than the Number type can safely store. It’s especially useful when working with large integers in applications like cryptography or financial calculations. BigInts are created by appending n to the end of an integer.

javascript
// Example of BigInt
let regularNumber = 9007199254740991; // Max safe integer in JS
let bigNumber = 9007199254740991000000000000000000n;

console.log(regularNumber);
console.log(bigNumber);

How It Works:

  • regularNumber holds the largest integer that can be accurately represented by the Number type.
  • bigNumber uses the n suffix to define a BigInt, allowing representation of much larger values.
  • The console.log() calls output both values to show the difference.

Output:

9007199254740991
9007199254740991000000000000000000n

Note: You cannot mix BigInt with regular Number types in arithmetic operations directly. You must convert one to match the other.


8. Object (object)

An object in JavaScript is a complex data type that allows you to store collections of data. It can hold multiple values of different types, such as strings, numbers, arrays, and even other objects. Objects are written as a list of key-value pairs enclosed in curly braces. Each key is a string (called a property) and each value can be any type of data.

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

How It Works:

  • person: This variable holds an object that contains three key-value pairs.
  • name: This key stores the string value "John".
  • age: This key stores the number value 30.
  • isStudent: This key stores the boolean value false.
  • The console.log() function prints the entire object to the console, showing all its key-value pairs.

Output:

{
  name: "John",
  age: 30,
  isStudent: false
}

9. Array (array (object type))

An array in JavaScript is a special type of object used to store multiple values in a single variable. These values are stored in an ordered list and can be accessed by their index (starting from 0). Arrays are written using square brackets [], with each value separated by a comma. Arrays can hold any type of data—strings, numbers, booleans, objects, or even other arrays.

javascript
// Example of an array
let colors = ["red", "green", "blue"];
let prices = [10, 45, 15];
console.log(colors);
console.log(prices);

How It Works:

  • colors: This variable holds an array with three string values.
  • "red", "green", "blue": These are elements of the array, stored in order.
  • You can access the first element using colors[0], the second with colors[1], and so on.
  • The console.log() function prints the entire array to the console.

Output:

["red", "green", "blue"]
[10, 45, 15]

Arrays are commonly used when you want to work with a list of items, such as numbers, strings, or objects.


Frequently Asked Questions

What are the different data types in JavaScript?

JavaScript has several data types including number, string, boolean, null, undefined, symbol, and BigInt. These are used to store different kinds of values in a program.


What is the difference between null and undefined in JavaScript?

undefined means a variable has been declared but not assigned a value. null is an assignment value that represents "no value". Both are different types in JavaScript.


What is a BigInt in JavaScript?

BigInt is a data type used to represent integers larger than the Number type can safely store. You define a BigInt by appending an n to the end of the number.


What is a Symbol in JavaScript?

Symbol is a primitive data type introduced in ES6. It is used to create unique identifiers for object properties to avoid naming conflicts.


Is JavaScript statically or dynamically typed?

JavaScript is dynamically typed, meaning variables can hold any type of value and their type can change at runtime without explicit type declarations.



What's Next?

Now that you know the basic data types in JavaScript, the next step is to learn how to work with them using operators and type conversion.