04 Js Variable Type

In JavaScript, variables can hold different types of data. Understanding data types is essential for working with JavaScript effectively. JavaScript has two main categories of data types: **Primitive types** and **Object types**.


 

### 1. Primitive Data Types
Primitive data types are the simplest forms of data. They are immutable, meaning their values cannot be changed once assigned.


 

- **String**: Represents a sequence of characters, used for text.
  ```javascript
  let name = "Alice"; // String
  ```


 

- **Number**: Represents both integer and floating-point numbers.
  ```javascript
  let age = 25;       // Integer
  let price = 19.99;  // Floating-point number
  ```


 

- **BigInt**: Used for very large integers, beyond the safe integer limit for `Number` (2^53 - 1).
  ```javascript
  let bigNumber = 1234567890123456789012345678901234567890n; // BigInt
  ```


 

- **Boolean**: Represents a logical entity and has two values: `true` and `false`.
  ```javascript
  let isStudent = true; // Boolean
  ```


 

- **Undefined**: A variable that has been declared but not assigned a value.
  ```javascript
  let score;
  console.log(score); // Outputs: undefined
  ```


 

- **Null**: Represents an intentional absence of any value. It’s a placeholder for “no value” and is distinct from `undefined`.
  ```javascript
  let response = null; // Null
  ```


 

- **Symbol**: Used for creating unique identifiers. Symbols are unique and immutable.
  ```javascript
  let id = Symbol("id");
  let anotherId = Symbol("id");
  console.log(id === anotherId); // Outputs: false (each Symbol is unique)
  ```


 

### 2. Object Types
Objects in JavaScript are collections of key-value pairs. They are mutable and can hold complex data and functions.


 

- **Object**: A complex data type used to store collections of data and more complex entities. Defined using curly braces `{}`.
  ```javascript
  let user = {
      name: "Alice",
      age: 25,
      isStudent: true
  };
  ```


 

- **Array**: A special type of object used to store ordered lists of values.
  ```javascript
  let colors = ["red", "green", "blue"]; // Array
  ```


 

- **Function**: Functions are first-class objects in JavaScript, meaning they can be assigned to variables and passed as arguments.
  ```javascript
  function greet(name) {
      return "Hello, " + name;
  }
  ```


 

### 3. Type Checking with `typeof`


 

You can check the type of a variable using the `typeof` operator:


 

```javascript
let name = "Alice";
console.log(typeof name); // Outputs: "string"


 

let age = 25;
console.log(typeof age); // Outputs: "number"


 

let isStudent = true;
console.log(typeof isStudent); // Outputs: "boolean"


 

let user = { name: "Alice", age: 25 };
console.log(typeof user); // Outputs: "object"


 

let colors = ["red", "green", "blue"];
console.log(typeof colors); // Outputs: "object" (Arrays are a type of object in JavaScript)


 

let greet = function() { return "Hello"; };
console.log(typeof greet); // Outputs: "function"
```


 

### 4. Example: Declaring and Using Different Variable Types


 

Here's a small program demonstrating different variable types:


 

```javascript
// Primitive types
let name = "Alice";        // String
let age = 30;              // Number
let bigNumber = 123456789012345678901n; // BigInt
let isStudent = false;     // Boolean
let notAssigned;           // Undefined
let emptyValue = null;     // Null


 

// Object types
let user = { name: "Alice", age: 30 }; // Object
let colors = ["red", "green", "blue"]; // Array
let greet = function() {               // Function
    return "Hello, JavaScript!";
};


 

// Display values
document.write("Name: " + name + "<br>");
document.write("Age: " + age + "<br>");
document.write("Big Number: " + bigNumber + "<br>");
document.write("Is Student: " + isStudent + "<br>");
document.write("Not Assigned: " + notAssigned + "<br>");
document.write("Empty Value: " + emptyValue + "<br>");


 

document.write("User: " + JSON.stringify(user) + "<br>");
document.write("Colors: " + colors.join(", ") + "<br>");
document.write("Greeting: " + greet() + "<br>");
```



 

Summary

Primitive Types: String, Number, BigInt, Boolean, Undefined, Null, Symbol.Object Types: Object, Array, Function.Understanding these types is crucial for working with data in JavaScript, as each type has unique properties and behaviors.

4 min read
Nov 19, 2024
By Md Real
Share

Leave a comment

Your email address will not be published. Required fields are marked *