Data types in javascript

Data types in javascript

Javascript data types

A. Primitive data types:

  1. String: String represents the sequence of the characters enclosed within a double quote or single quote

     let StringText= "Hello there";
     //if we try to console.log the variable it will give output as Hello there
     console.log(StringText);
    
     //if we check the type of the variable it will give output as String
     console.log(typeof StringText);
    
  2. Number: Number represents numeric values which include integers as well as floating-point numbers

     let age=26
     let weight=55.3
     console.log(age);
     console.log(weight);
     //if we try to console.log the variable it will give output as 26 and 55.3 respectively
     console.log(typeof age);
     console.log(typeof weight);
     //if we check the type of the variable it will give output as number
    
  3. Boolean: Boolean represents a binary value, either 1 or 0 i.e. true or false, and is often used for making logical decisions.

     let isTrue = true;
     let isFalse = false;
     console.log(isTrue);
     console.log(isFalse);
     //if we try to console.log the variable it will give output as true, false respectively
     console.log(typeof isTrue);
     console.log(typeof isFalse);
     //if we check the type of the variable it will give output as Boolean
    
  4. Undefined: Undefined represents the variable that has been declared but doesn't have any value assigned at this point

     let undefinedVar;
     //here we just declared the variable but haven't assigned any value yet
     console.log(undefinedVariable);
     //if we try to console.log the variable it will give output as undefined
     console.log(typeof undefinedVariable);
     //if we check the type of the variable it will also return undefined
    
  5. Null: Null Represents the intentional absence of any object value or the lack of a value.

     let nullValue = null;
     console.log(nullValue);
     //if we try to console.log the variable it will give output as null
     console.log(typeof nullValue);
     //if we check the type of the variable it gives output as Object
    

    This might seem surprising, but it's a known quirk in JavaScript. The typeof null returns 'object' '. This is considered an error in the design of JavaScript, as null is not an object, but rather a primitive value representing the intentional absence of any object value.

  6. Symbol: A symbol represents a unique and immutable value often used as object property keys.

     const uniqueSymbol = Symbol("description");
    
     console.log(uniqueSymbol);
     //if we try to console.log the variable it will give output as Symbol(description)
     console.log(typeof uniqueSymbol);
     //if we check the type of the variable it gives as Symbol
    

    This output indicates that uniqueSymbol is a Symbol with the description "description." Each Symbol in JavaScript is unique, and the description is a label for the Symbol, but it doesn't affect its uniqueness. Symbols are often used as unique keys for object properties and to create private or hidden object properties in JavaScript.

B. Non-primitive data types:

  1. Functions: Functions are also objects in JavaScript. They can be assigned to variables, passed as arguments, and returned from other functions. Functions are used to define behavior and encapsulate code.

     //Function declaration
     const myFunction = function(){
         console.log("Hello World");
     }
     //Function call
     myFunction() //Output: Hello World
    
     //Function with arguments
     const AddFunction = function(val1,val2){
         console.log(val1+val2);
     }
     AddFunction(3,4)//Output: 7
    
     //Function with return
     const MultiplyFunction = function(val1,val2){
         return val1*val2
     }
     let result = MultiplyFunction(3,4)
     console.log(result) //Output: 12
    
  2. Object: Objects are the most common non-primitive data type in JavaScript. They are collections of key-value pairs and can represent complex structures. Objects can contain properties and methods.

     //object declaration
     let myObj = {
         name:"udayraj",
         age: 22
     }
     //access the object values
     console.log(myObj.age) //Output:22
    

    We can also declare a function inside an object as shown below

     const myObject = {
       greeting: "Hello",
       sayHello(name) {
         console.log(this.greeting + ", " + name);
       },
     };
     console.log(myObject.sayHello("Uday")); //Output: Hello, Uday
    
  3. Arrays: Arrays are a special type of object that holds an ordered list of values. They are often used for collections of items. Arrays can hold data of similar data types as shown below

     //array declaration
     let superheroes= ["Batman", "Logan", "Spiderman"];
     //access element from array
     console.log(superheroes[1] //output: Logan
    

    Arrays can also contain a mix of different data types as shown below

     let superheroFeatures= ["Batman", "batmobile", 45, true];