07 - JavaScript - DataTypes

07 - JavaScript - DataTypes

In JavaScript, a data type is a classification identifying the type of value that a variable can hold. JavaScript has six primitive data types which are Boolean, Null, Undefined, Number, String, and Symbol. Primitive data types are immutable, which means that they cannot be changed. In addition to the primitive data types, JavaScript also has a reference data type, which is Object. Objects are used to store collections of key-value pairs and are mutable, meaning they can be modified. Understanding data types is crucial for writing effective JavaScript code as it helps in memory allocation and optimization.

Different Types of DataTypes

let's dive into each data type in JavaScript in more detail -

  1. String: A string is a sequence of characters enclosed in quotes, either single or double. Strings are used to represent text in JavaScript. For example:

     let name = "John";
    
  2. Number: A number is a numeric value in JavaScript. Numbers can be integers or floating-point numbers. For example:

     let age = 25;
    
  3. BigInt: A BigInt is a numeric value that can represent numbers larger than 2^53-1. BigInts are denoted by adding an n at the end of an integer. For example:

     let bigNumber = 9007199254740991n;
    
  4. Boolean: A Boolean is a data type that represents either true or false. For example:

     let isStudent = true;
    
  5. Undefined: Undefined is a primitive value that represents an uninitialized variable. If a variable is declared but not assigned a value, it is undefined. For example:

     let height;
     let weight = undefined;
    
  6. Null: Null is a primitive value that represents the intentional absence of any object value. For example:

     let score = null;
     let value = undefined;
    
  7. Array: An array is a collection of elements, stored in a single variable. Arrays are used to store a collection of related data items. For example:

     let fruits = ["apple", "banana", "orange"];
    
  8. Date: A Date represents a specific moment in time. Dates are used to perform calculations and comparisons with dates and times. For example:

     let today = new Date();
    
  9. Object: An object is a composite value that can contain properties and methods. Objects are used to represent complex data structures. For example:

     let person = {
       name: "Jane",
       age: 30,
       hobbies: ["reading", "hiking"],
       address: {
         street: "123 Main St",
         city: "Seattle",
         state: "WA"
       },
       sayHello: function() {
         console.log("Hello!");
       }
     };
    
  10. Symbol: Symbols are new primitive values introduced in ECMAScript 2015. A Symbol is a unique identifier that can be used as a property key in objects. For example:

    const id = Symbol(); let person = {     [id]: "1234",     name: "John" }; console.log(person[id]); // "1234"
    

Summarizing Up

JavaScript has six primitive data types, including Boolean, Null, Undefined, Number, String, and Symbol, and one reference data type, Object. Data types in JavaScript classify the type of value that a variable can hold, and primitive data types are immutable while reference data types are mutable. Understanding data types is important in optimizing memory allocation and writing effective code.

Did you find this article valuable?

Support Bosonique ITEdTech by becoming a sponsor. Any amount is appreciated!