What are Data Types in JavaScript?

  1. Data types represent the type of information a variable has.
  2. Every programming language has its own set of data types.
  3. JavaScript also has several data types, but as programmers, we usually don’t declare them explicitly.
  4. JavaScript is a loosely typed scripting language, so data type declaration is not required. The data type is determined internally at runtime.
  5. In TypeScript, we can declare data types explicitly because it is a stricter superset of JavaScript. Therefore, it is important to understand data types.

How Are Data Types Classified in JavaScript?

In JavaScript, data types are mainly classified into two categories:

  1. Primitive Data Types
    1. String
    2. Number
    3. BigInt
    4. Boolean
    5. Undefined
    6. Null
    7. Symbol
  2. Non-Primitive Data Types
    1. Object
    2. Array
    3. Function
    4. Date
    5. Map
    6. Set
    7. RegExp

String

  1. In JavaScript, a string is used to represent textual information, similar to other programming languages.
  2. JavaScript supports different ways of declaring strings.
  3. For example: "Hello", 'Hello', and `Hello`.
let name = "John";
let city = 'Mumbai';
let message = `Hello ${name}`;
let name = "Akshay";
let age = 30;

console.log(`My name is ${name} and I am ${age} years old.`);

Number

  1. JavaScript has Number type for both integers and decimals.
  2. JavaScript doesn’t have separate int, float, and double types in normal use.
  3. E.g. 
let age = 30;
let price = 99.99;
let temperature = -10;

BigInt

  1. Number can not safely represent arbitrarily large numbers.
  2.  For very large integers JavaScript provides BigInt.
  3. To represent BigInt number its required to use ‘n’ as suffix at the end.
  4. E.g. let bigNumber = 123456789012345678901234567890n;

Boolean

  1. Boolean has only two values i.e. true and false.
  2. It is also known as toggle data type which is mostly used in decision making statements to check the conditions.
  3. Boolean values are extremely important in JavaScript because many values can be automatically converted to true or false.
  4. E.g.
    1. let isLoggedIn = true;
    2. let isAdmin = false;

Undefined

  1. Undefined generally means variable exists but doesn’t have any value.
  2. let name;
    console.log(name);
  3. This will produce output as Undefined.

Null

  1. Null means there is intentionally no value.
  2. let selectedUser = null;
  3. Strange JavaScript behavior
    1. typeof null
    2. Returns object
  4. Even though null is a primitive value, typeof null says “object”.

Symbol

  1. Symbol is primarily used for creating “unique identifiers”.
  2. let id1 = Symbol("id");
    let id2 = Symbol("id");
    console.log(id1 === id2);
  3. This will return false even though both have description “id” they are Symbols. 
  4. If you print typeof(id1) then output will be Symbol.

Object

  1. This is the most important data-type to understand in JavaScript.
  2. An Object stores data in key-value pairs.
  3. let person = {

    name: "Akshay",

    age: 30,

    city: "Mumbai"

    };
  4. To access the value of particular key inside the object use property name with object reference.
  5. console.log(person.name);
  6. Arrays, functions, dates, maps, sets, etc. are technically built around objects.
Share.
Leave A Reply

Exit mobile version