In this shot, we will go through the differences of two data types, any
and Object
.
any
var 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 type var data:any; //assigning string to data data = "Data from server" //priting data to console console.log(data)
We can re-assign different values of different data types to variable data
.
// creating variable with any data type var data:any; //assigning string to data data = "Data from server" //priting data to console console.log(data) //reassigned with integer data = 10 console.log(data) //reassigned with boolean data = true console.log(data)
Object
var 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 Object var fruits:Object; //assigning object to fruits fruits = { 1:"Apple", 2:"Banana", 3:"Orange" } //pritintg fruits Object console.log(fruits)
We can access the values present in the Object
with keys.
//creating variable fruits of data type Object var fruits:Object; //assigning object to fruits fruits = { 1:"Apple", 2:"Banana", 3:"Orange" } //get value for key 1 console.log(fruits["1"])
RELATED TAGS
CONTRIBUTOR
View all Courses