JavaScript : Data Types

JavaScript : Data Types

Kinds of data JavaScript can handle.

What are data types?

The data types are just categorizations of data into different types depending upon various use cases. Javascript categorizes data into two types.

  1. Primitive - Immutable values

    • number

    • string

    • boolean

    • bigInt

    • null

    • undefined

    • symbol

  2. Object - Anything which is not primitive.

Number

Integers or floating point numbers. In JavaScript, primarily they are defined using Number Literals.

Examples: 123, 12.3

Note: BigInt is an extension of the Number data type with high precision.

String

Text data. In JavaScript, strings are primarily defined using String Literals.

Examples: "Hello" 'Hello'

Boolean

Logical values - True or False also called Boolean Literals. Booleans are usually used in combination with control statements like if-else, for loop, switch etc.

Null and Undefined

These are special values used in different use cases. The most common use case is to use them as a pre-initialized value for a variable.

null indicates the absence of an object value, which could be defined later.

undefined indicates the absence of a primitive value, which could be defined later.

Object

Objects are mutable, unlike primitive values. We define an object in the form of key & value pairs. Each pair is called a property. Where a property key could be any valid JavaScript identifier. A property value could be any valid JavaScript datatype including objects.

Example

{
    key1:"Hello"  //string as value
    key2:123      //number as value
    key3:true     //boolean as value
    key4:{ }      //object as value
}

Property value could be a function also ( in JavaScript functions are callable objects), we call them methods.

{
    firstName:"Subham",                       
    lastName:"Sahu",
    getFullName: function () {                // method
        return this.firstName + this.lastName;
    }    
}

For you to explore.

Learn about,

  1. symbol data type.

  2. typeof Operator.

What Next?

  • What are other ways to define each data type?

  • Can we convert one data type to another?