Instagram
youtube
Facebook
Twitter

JavaScript Data Types

In JavaScript, data types are fundamental concepts that every programmer must understand. They define the kind of data that a variable can hold, which is crucial for manipulating and interacting with data effectively. This lesson will introduce you to the different data types in JavaScript, how to use them, and why they are essential in programming.


1. What are Data Types?

Data types specify the kind of values a variable can hold. Knowing the data type of a variable is essential as it determines how you can use and manipulate that data within your program.


2. Primitive Data Types

JavaScript includes several primitive (basic) data types. These are the simplest types of data you can work with:

  • String: Used to represent textual data. A string is a sequence of characters enclosed in single (') or double (") quotes.

    • Example:
      let name = "Mradul";

       

  • Number: Represents both integers and floating-point numbers. In JavaScript, there is no separate type for integers and decimals.

    • Example:
      let age = 25;

       

  • Boolean: Represents logical values, which can be either true or false.

    • Example:
      let isStudent = true;

       

  • Null: Represents an intentional absence of any object value. It is commonly used to indicate that a variable is empty or not yet assigned.

    • Example:
      let car = null;

       

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

    • Example:
      let score;

       

  • Symbol: Introduced in ECMAScript 6, symbols are unique and immutable, often used as object property keys to avoid naming conflicts.

    • Example:
      let symbol = Symbol("unique");

3. Non-Primitive (Reference) Data Types

In addition to primitive data types, JavaScript supports non-primitive (reference) data types, which are more complex:

  • Object: A collection of properties, where each property is a key-value pair. Objects are a fundamental building block in JavaScript.

    • Example: 
      let person = { 
         name: "Mradul",
         age: 30 
      };

       

  • Array: A special type of object that represents a list of values. Each value can be accessed by its index, starting from zero.

    • Example:
      let colors = ["red", "green", "blue"];

       

  • Function: A block of reusable code that performs a specific task. Functions in JavaScript are also considered objects.

    • Example:
      function greet() {
       return "Hello, World!";
       }

4. Type Conversion

JavaScript automatically converts between different data types when necessary, known as type conversion.

  • Implicit Conversion: JavaScript automatically changes the data type when needed, such as converting a number to a string during concatenation.

    • Example:
      let result = "The answer is " + 42; // "The answer is 42"

       

  • Explicit Conversion: You can manually convert data types using functions like String(), Number(), and Boolean().

    • Example:
      let num = 100; 
      let str = String(num); // "100"

5. Checking Data Types

You can check the data type of a variable or value using the typeof operator.

  • Example:
    let score = 90; 
    console.log(typeof score); // "number"

6. Understanding and Handling NaN

"NaN" stands for "Not-a-Number" and is a special value that represents a failed numeric operation.

  • Example:
    let result = 0 / 0; // NaN 
    console.log(isNaN(result)); // true

Conclusion

Understanding data types is crucial for writing effective JavaScript code. By mastering data types, you’ll be able to manage variables more effectively, perform proper data manipulation, and avoid common errors in your programs. This knowledge forms the foundation for all your future programming in JavaScript.