What is data type in JavaScript and how many types?

In JavaScript, a data type is a type of value that can be stored in a variable. There are a total of 7 data types in JavaScript:

  1. Number: used to represent numeric values, including integers and floating-point numbers.
  2. String: used to represent sequences of characters, such as words or sentences.
  3. Boolean: used to represent the truth values of true or false.
  4. Null: used to represent an intentional non-value.
  5. Undefined: used to represent an uninitialized value.
  6. Object: used to represent collections of data and properties, as well as methods.
  7. Symbol: used to represent unique and immutable data types.

There are two main categories of data types: primitive and non-primitive.

Primitive data types are the basic data types that form the building blocks of the language. They include:

  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined
  6. Symbol

These data types are simple, immutable, and have a direct value stored in the memory. When a primitive value is assigned to a variable, the value is stored directly in the memory and not as an object reference.

On the other hand, non-primitive data types are more complex and dynamic. They are objects, which are instances of object constructors, and include:

  1. Object
  2. Array
  3. Function
  4. RegExp (regular expression)

These data types are mutable, meaning that their properties and values can be changed after they have been created. When a non-primitive value is assigned to a variable, the variable stores a reference to the object in the memory, rather than the actual value itself.

Similar Posts

Leave a Reply

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