In this shot, we will go through the differences of two data types, any and Object.
anyvar variablename:any;
any to use when we don’t know the type of data that the variable holds while writing code.any.var data:any;
any.var inputFromUser:any
In the following code snippet, we created a variable data with type any, and then we assign the string Data from server to data.
// creating variable with any data typevar data:any;//assigning string to datadata = "Data from server"//priting data to consoleconsole.log(data)
We can re-assign different values of different data types to variable data.
// creating variable with any data typevar data:any;//assigning string to datadata = "Data from server"//priting data to consoleconsole.log(data)//reassigned with integerdata = 10console.log(data)//reassigned with booleandata = trueconsole.log(data)
Objectvar a: Object
Object to store plain JavaScript objects.key-value pairs separated by a comma ,.{
1:"apple",
2:"Banana"
}
In the following example, we are creating a variable fruits with data type as Object, and assigning values to it in line 5.
//creating variable fruits of data type Objectvar fruits:Object;//assigning object to fruitsfruits = {1:"Apple",2:"Banana",3:"Orange"}//pritintg fruits Objectconsole.log(fruits)
We can access the values present in the Object with keys.
//creating variable fruits of data type Objectvar fruits:Object;//assigning object to fruitsfruits = {1:"Apple",2:"Banana",3:"Orange"}//get value for key 1console.log(fruits["1"])