Hello. I’m wanting to have an array called coronaCases that has nested variables that are objects. It however says that there is an error with var januaryMonth (I’m assuming it’s a syntax issue). I’ve looked at it and know something’s wrong, but can’t quite figure it out. Here’s my code:
var coronaCases = [
var januaryMonth = {
monthCases : 11950, //11,950
current : 11950 //11,950
},
var febuaryMonth = {
monthCases : 74656, //74,656
current : 86606 // 86,606
}
]
Yes this is not possible in JavaScript and other languages. You can’t really declare a variable inside an array. If you want to access januaryMonth and others inside coronaCases, what you can do instead of an array using objects and then put it as properties. Here’s an example:
var monthOfTheYear = {
january: {
days: 31,
weeks: 4
},
february: {
days: 29,
weeks: 3
}
};
//Then you can access them using the bracket notation
//or the dot notation
console.log(monthOfTheYear.january)
//Will return:
/*
{
days: 31,
weeks: 4
}
*/