Javascript

JavaScript supports several data types that are used to represent different kinds of values. Here's an overview of the main data types in JavaScript:

  1. Primitive Data Types:

    a. Number: - Represents numeric values, including integers and floating-point numbers. javascript var age = 30; var price = 19.99;

    b. String: - Represents textual data enclosed within single ('') or double ("") quotes. javascript var name = "John"; var message = 'Hello, world!';

    c. Boolean: - Represents logical values true or false. javascript var isFound = true; var isExpired = false;

    d. Null: - Represents an intentional absence of any object value. javascript var emptyValue = null;

    e. Undefined: - Represents a variable that has been declared but has not been assigned a value yet. javascript var notDefined;

    f. Symbol (added in ECMAScript 6): - Represents a unique identifier that is used to avoid naming conflicts.

  2. Non-Primitive Data Types:

    a. Object: - Represents a collection of key-value pairs where keys are strings and values can be of any data type, including other objects. javascript var person = { name: "John", age: 30, isStudent: false };

    b. Array: - Represents an ordered list of values, where each value can be of any data type, including other arrays and objects. javascript var numbers = [1, 2, 3, 4, 5]; var fruits = ["apple", "banana", "orange"];

  3. Type Conversion:

    • JavaScript is a loosely typed language, meaning variables can hold values of any data type. JavaScript also performs automatic type conversion (coercion) when necessary.
    javascript
    var num = 10; var str = "20"; var result = num + str; // Result: "1020"
  4. Type Checking:

    • You can use the typeof operator to check the data type of a variable or expression.
    javascript
    var num = 10; var type = typeof num; // Result: "number"

Understanding JavaScript data types is crucial for writing efficient and bug-free code. It's essential to know how to work with different data types and how they behave in various contexts.

Thanks for learning