Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

JavaScript Data Types Explained: Strings, Numbers, Booleans & More

Posted on April 2, 2025 • 5 min read • 1,002 words
Share via
Mapagam
Link copied to clipboard

Learn about JavaScript data types like strings, numbers, booleans, objects, arrays, and more. Master type conversion and improve coding skills.

On this page
1. Introduction to JavaScript Data Types 1.1. Primitive Data Types 1.1.1. Strings 1.1.2. Numbers 1.1.3. Booleans 1.1.4. Undefined 1.1.5. Null 1.1.6. Symbols (ES6 and Later) 1.2. Non-Primitive Data Types 1.2.1. Objects 1.2.2. Arrays 1.2.3. Functions 1.2.4. Special Case: Date and RegExp 2. Type Conversion in JavaScript 2.1. Implicit Type Conversion (Type Coercion) 2.2. Explicit Type Conversion 3. Conclusion

JavaScript is one of the most widely used programming languages, powering everything from simple web applications to complex, high-performance platforms. One of the core concepts in JavaScript is understanding data types, as they form the foundation for how data is stored and manipulated in your code.

1. Introduction to JavaScript Data Types

In JavaScript, data types can be divided into two categories: Primitive Types and Non-Primitive Types.

  • Primitive types are immutable and represent single values. These include:

    • Strings
    • Numbers
    • Booleans
    • Undefined
    • Null
    • Symbols (ES6 and later)
  • Non-primitive types (or Reference Types) represent more complex entities and include:

    • Objects
    • Arrays
    • Functions

Understanding these types and their characteristics is crucial for mastering JavaScript programming.

1.1. Primitive Data Types

1.1.1. Strings

A string is a sequence of characters enclosed within either single quotes (' ') or double quotes (" "). JavaScript treats both single and double quotes equally, and you can use whichever suits your style.

Examples:

let message = "Hello, World!";
let name = 'John Doe';

Strings in JavaScript are immutable, meaning once a string is created, it cannot be changed directly. However, you can perform operations to generate new strings.

Common String Methods:

  • length: Returns the length of a string.
    console.log(message.length); // 13
    
  • toUpperCase(): Converts a string to uppercase.
    console.log(name.toUpperCase()); // 'JOHN DOE'
    
  • charAt(): Retrieves a character at a specified index.
    console.log(message.charAt(0)); // 'H'
    

1.1.2. Numbers

A number in JavaScript represents both integers and floating-point numbers (decimals). JavaScript uses a double-precision floating-point format to represent all numbers, which means it can handle very large or very small numbers, but it might lose precision with very large values.

Examples:

let age = 25;
let price = 19.99;

JavaScript offers several mathematical operators and functions that make working with numbers easy:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)

Example:

let sum = 10 + 5; // 15
let product = 10 * 5; // 50

1.1.3. Booleans

A boolean is a simple data type that can only hold one of two values: true or false. Booleans are primarily used in conditional statements (like if and while) to control the flow of a program.

Examples:

let isJavaScriptFun = true;
let isWeatherSunny = false;

Booleans are often the result of comparison operators like ==, !=, >, <, etc.

Example:

let x = 10;
let y = 5;

console.log(x > y); // true

1.1.4. Undefined

A variable that has been declared but not assigned a value automatically has the undefined data type. It is a special value in JavaScript that represents the absence of a defined value.

Example:

let user;
console.log(user); // undefined

In this example, user is declared but not assigned any value, so its value is undefined.

1.1.5. Null

Null is another primitive data type in JavaScript. It is intentionally assigned to a variable to indicate that the variable should have no value.

Example:

let person = null;
console.log(person); // null

Although null is a value, it is often used to signify the absence of any object or data.

1.1.6. Symbols (ES6 and Later)

A Symbol is a unique and immutable data type introduced in ES6. Symbols are often used as identifiers for object properties. They are particularly useful for creating private or hidden properties in objects.

Example:

let uniqueSymbol = Symbol('description');

Symbols are not comparable, meaning two symbols with the same description are not equal to each other.

1.2. Non-Primitive Data Types

1.2.1. Objects

An object in JavaScript is a collection of key-value pairs, where the keys (also known as properties) are strings, and the values can be any valid data type (including other objects or functions). Objects allow you to store and organize complex data.

Example:

let person = {
  name: 'John Doe',
  age: 30,
  isEmployed: true
};

You can access object properties using dot notation or bracket notation.

Example:

console.log(person.name); // John Doe
console.log(person['age']); // 30

1.2.2. Arrays

An array is a special type of object that holds a list of values in a single variable. Arrays can store values of different types, and their elements are ordered by index (starting from 0).

Example:

let fruits = ['apple', 'banana', 'cherry'];

You can access elements in an array using their index:

Example:

console.log(fruits[0]); // apple
console.log(fruits[1]); // banana

1.2.3. Functions

In JavaScript, functions are also considered objects. A function is a block of reusable code designed to perform a specific task. Functions can accept input in the form of parameters and can return output.

Example:

function greet(name) {
  return 'Hello, ' + name + '!';
}

console.log(greet('John')); // Hello, John!

1.2.4. Special Case: Date and RegExp

In addition to regular objects, JavaScript also provides built-in objects like Date and RegExp.

  • Date: Used for working with dates and times.

    let currentDate = new Date();
    console.log(currentDate);
  • RegExp: Used for pattern matching with strings.

    let regex = /abc/;
    let testString = 'abcdef';
    console.log(regex.test(testString)); // true
    

2. Type Conversion in JavaScript

JavaScript is a loosely typed or dynamically typed language, which means that the data type of a variable is determined at runtime. However, sometimes it’s necessary to convert one data type to another. JavaScript provides type conversion mechanisms to handle this.

2.1. Implicit Type Conversion (Type Coercion)

JavaScript often performs implicit type conversion automatically when different types are used together. This can sometimes lead to unexpected results.

Example:

let result = '5' + 10; // '510'

In this case, JavaScript converts the number 10 into a string and performs string concatenation.

2.2. Explicit Type Conversion

You can explicitly convert a variable to another data type using functions like:

  • String(): Converts to a string.
    let num = 10;
    let str = String(num); // '10'
    
  • Number(): Converts to a number.
    let str = '25';
    let num = Number(str); // 25
    
  • Boolean(): Converts to a boolean.
    let value = 0;
    let boolValue = Boolean(value); // false
    

3. Conclusion

Understanding JavaScript’s data types is crucial for effective programming. JavaScript is a loosely typed language, so type conversion (implicit and explicit) is often involved in operations. Make sure to carefully consider the types of variables you work with to avoid unexpected behaviors and bugs in your code.

JavaScript Data Types   Strings   Numbers   Booleans   Type Conversion  
JavaScript Data Types   Strings   Numbers   Booleans   Type Conversion  
 Let vs Var vs Const: Choosing the Right JavaScript Variable
What Are Variables in JavaScript? A Simple Explanation 

More Reading!

  1. What Are Variables in JavaScript? A Simple Explanation
On this page:
1. Introduction to JavaScript Data Types 1.1. Primitive Data Types 1.1.1. Strings 1.1.2. Numbers 1.1.3. Booleans 1.1.4. Undefined 1.1.5. Null 1.1.6. Symbols (ES6 and Later) 1.2. Non-Primitive Data Types 1.2.1. Objects 1.2.2. Arrays 1.2.3. Functions 1.2.4. Special Case: Date and RegExp 2. Type Conversion in JavaScript 2.1. Implicit Type Conversion (Type Coercion) 2.2. Explicit Type Conversion 3. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard