Javascript Data Types

Javascript Data Types

ยท

2 min read

In Programming, Data Types are the most important for defining variables, in any other programming language for storing different data types we have different things but in JS they can hold any type of data.
That's why Javascript is called "Loosely Type Language"

Javascript has mainly 8 Datatypes -

  1. String

  2. Number

  3. Boolean

  4. Undefined

  5. Null

  6. BigInt

  7. Symbol

  8. Object

Let's see how to store these data types in Javascript

  1. String-

    It represents the sequence of characters.

     let greeting = "Hello, World!";
     let name = 'John Doe';
    
  2. Number-

    It represents numeric values, including integers and floating-point numbers.

     let integerNumber = 42;
     let floatingPointNumber = 3.14;
    
  3. Boolean -

    It represents a binary value, either true or false.

     let isProgrammingFun = true;
     let isRainyDay = false;
    
  4. Undefined -

    It represents a variable that has been declared but not assigned a value.

     let a;
     console.log(a); // Output: undefined
    
  5. Null -

    It represents the intentional absence of any object value.

     let Value = null;
    
  6. BigInt -

    It was introduced in ECMAScript 2020, It is used for representing integers of arbitrary precision.

     let storeBigVal= 1885699254740991n;
    
  7. Symbol -

    It's Introduced in ECMAScript 6, symbols are unique and immutable data types

     const sampleSymbol = Symbol("description");
    
  8. Object -

    It represents a collection of key-value pairs and is a fundamental data structure in JavaScript.

     let Person = {
       name: "Saurav",
       age: 20,
       isStudent: true
     };
    

Type Coercion

Type coercion is the automatic conversion of one data type to another in a programming language.

JavaScript, being a loosely typed language, performs type coercion in certain situations, allowing operations between different data types without explicit conversion by the developer.

let num = 10;        // a number
let str = "5";       // a string containing a number

let result = num + str;  
console.log(result);    // Output: 105

In this example, We have a number (num) and a number-as-text (str). When we use '+' in JavaScript, it turns the text into a number, but it also works as a joiner. So, instead of adding, it combines them, resulting in '105'.

Type of Operator

The typeof operator in JavaScript helps you find out the data type of a value or variable. It's like asking, "What kind of thing is this?"

let num = 42;
console.log(typeof num); // Output: "number"

Here, typeof tells us that the data type of num is a "number.

Thanks for Checking this, Happy Coding ๐ŸŽ‰

ย