In JavaScript, data types define the types of values that can be stored and manipulated in variables. JavaScript has several built-in data types that can be categorized into the following groups:

1. Primitive Data Types: These are the basic building blocks of data. They are immutable (cannot be changed) and directly store their values.

  • Number: Represents both integer and floating-point numbers.

let integer = 10;
let floatingPoint = 3.14;

 

  • String: Represents a sequence of characters.

let name = "John";
let message = "Hello, " + name;

 

  • Boolean: Represents a logical value, either true or false.

let isTrue = true;
let isFalse = false;

 

  • Undefined: Represents a variable that has been declared but not assigned a value.

let undefinedVariable;

 

  • Null: Represents the intentional absence of any value.

let emptyValue = null;

 

  • BigInt: Represents arbitrarily large integers.

let bigIntValue = 1234567890123456789012345678901234567890n;

 

  • Symbol: Represents a unique and immutable value used to identify object properties.

let uniqueSymbol = Symbol("description");

 

2. Composite Data Types: These are composed of multiple primitive or other composite data types. They are mutable (can be changed) and can hold more complex data structures.

  • Object: Represents a collection of key-value pairs, where keys are strings (property names) and values can be of any data type.

let person = {
    name: "Alice",
    age: 30,
    isStudent: false
};

 

  • Array: Represents an ordered list of values, accessed by their numerical indices.

let numbers = [1, 2, 3, 4, 5];

 

  • Function: Represents a block of reusable code that can be invoked.

function add(a, b) {
    return a + b;
}

 

3. Special Data Types: These are not primitive or composite types but rather specific values that have special meaning.

  • NaN: Represents a value that is "Not-a-Number" when a mathematical operation doesn't produce a meaningful result.

let result = "abc" / 2;
console.log(result); // NaN

 

  • Infinity and -Infinity: Represent positive and negative infinity, often resulting from mathematical operations that exceed the limits of the floating-point representation.

let positiveInfinity = Infinity;
let negativeInfinity = -Infinity;

 

JavaScript's dynamic typing allows variables to hold values of different data types without explicit type declaration. For example, a variable that initially holds a number can later be assigned a string. However, the behavior of variables in such cases can lead to unexpected results, so it's important to understand the data types and their behaviors to write robust code.

Remember that data types play a crucial role in determining how values can be manipulated and interacted with in your JavaScript programs.

Latest Tutorial

Newly published tutorials

How to Make an AWS S3 Bucket Public

Learn how we can make an AWS S3 bucket public

Laravel 10 - Send mail using AWS SES

Learn to send Email using Laravel 10 with AWS SES service.

Laravel 10 - Tutorial on how to use jQuery to validate the form

Learn how to use jQuery in Laravel 10 to validate the form