JavaScript Data Types

 

In JavaScript, data types are the different kinds of values you can work with. Let's break them down:

1. Primitive Types (Simple data types)

These are basic types of data that are not objects and have no methods.

  • Undefined: This means a variable has been declared but not yet given a value.
    • Example: let a; (Here, a is undefined because it doesn't have a value yet.)
  • Null: This represents an intentional absence of any object value.
    • Example: let b = null; (Here, b is set to nothing.)
  • Boolean: This can only be true or false.
    • Example: let isSunny = true;
  • Number: This is used for both integers and floating-point numbers (decimals).
    • Example: let age = 25;, let price = 19.99;
  • BigInt: This is used for really large numbers that can’t be represented by the Number type.
    • Example: let bigNumber = 1234567890123456789012345678901234567890n;
  • String: This is used for text, written inside quotes.
    • Example: let name = "John";
  • Symbol: This is used to create unique identifiers for objects. You don't need to worry about this much as a beginner.

2. Non-Primitive Types (Complex data types)

These are more complex and can hold collections of values.

  • Object: This can store collections of data and more complex entities. Objects can hold properties (key-value pairs).
    • Example:
    • let person = {
      name: "John",
      age: 30
      };
  • Arrays: These are special kinds of objects that hold lists of values.
    • Example: let numbers = [1, 2, 3, 4, 5];
  • Functions: Functions are also objects, but they can be called to perform actions.
    • Example:
    • function greet() {
      return "Hello!";
      }
Powered by Blogger.