AskHandle

AskHandle Blog

JavaScript Data Types

September 4, 2025Alicia Gopin3 min read

JavaScript Data Types

JavaScript is a versatile programming language widely used for web development. One key area for developers to understand is data types. Knowing data types is essential as it affects how values are stored and manipulated. This article covers the various data types in JavaScript, their characteristics, and examples of their usage.

Primitive Data Types

JavaScript data types are grouped into two main categories: primitive and reference data types. Let's first look at the primitive data types.

1. Number

The Number data type represents both integer and floating-point numbers. JavaScript treats all numbers as floating-point values. Here's an example:

javascript
1let num = 10;
2console.log(num); // Output: 10

2. String

The String data type is used for text values enclosed within single ('') or double ("") quotes. Strings can include letters, numbers, symbols, and whitespace characters. Here's an example:

javascript
1let message = "Hello, World!";
2console.log(message); // Output: Hello, World!

3. Boolean

The Boolean data type represents logical values of true or false. Booleans are often used in conditional statements and comparisons. Here's an example:

javascript
1let isTrue = true;
2console.log(isTrue); // Output: true

4. Undefined and Null

The undefined data type represents a variable that has been declared but not assigned a value. The null data type signifies an intentional absence of any value. Here's an example showcasing the difference:

javascript
1let x;
2console.log(x); // Output: undefined
3
4let y = null;
5console.log(y); // Output: null

5. Symbol

The Symbol data type represents unique and immutable values often used as object property keys. Symbols help ensure the uniqueness of properties. Here's an example:

javascript
1const id = Symbol('id');
2console.log(id); // Output: Symbol(id)

Reference Data Types

Reference data types in JavaScript include objects, arrays, and functions. These types can hold multiple values and reference complex data structures.

1. Object

The Object data type is a collection of key-value pairs, where values can be of any data type. Objects are commonly used for organizing and manipulating data. Here's an example:

javascript
1const person = {
2  name: 'Alice',
3  age: 30,
4  city: 'London'
5};
6
7console.log(person.name); // Output: Alice

2. Array

The Array data type stores lists of values indexed by positions. Arrays can contain elements of any data type, making them useful for grouping related data. Here's an example:

javascript
1const colors = ['red', 'green', 'blue'];
2console.log(colors[0]); // Output: red

3. Function

Functions in JavaScript are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions. Functions are crucial for building reusable and modular code. Here's an example:

javascript
1function greet(name) {
2  return `Hello, ${name}!`;
3}
4
5console.log(greet('Bob')); // Output: Hello, Bob!

Type Coercion

JavaScript is a loosely typed language, which means data type conversion can happen implicitly during operations. This behavior, known as type coercion, can lead to unexpected results if not understood. Here's an example of type coercion:

javascript
1const result = 10 + '5';
2console.log(result); // Output: 105 (string concatenation)

Checking Data Types

You can check the data type of a value using the typeof operator. This operator returns a string indicating the type of the operand. Here are some examples:

javascript
1console.log(typeof 42); // Output: number
2console.log(typeof 'Hello'); // Output: string
3console.log(typeof true); // Output: boolean

Understanding JavaScript data types is vital for writing reliable code. Knowing the intricacies of primitive and reference data types helps developers manipulate values, avoid unexpected behaviors, and build scalable applications.