Search code examples
javascriptsquare-bracket

Please can someone explain what the second square bracket notation is doing here?


function(today) {
    var weekFromToday = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000); 
    var dayNames = ["x","y", "z"]; 
    var day = dayNames[weekFromToday.getDay()]; 
}

The square brackets in the third var do not refer to an index number, what does the notation mean in this case?


Solution

  • It works like any other array value getting accessed via index.

    Here weekFromToday.getDay() returns an integer from 0-6 and if

    var dayNames = ["x","y", "z"]; 
    

    were

    var dayNames = ["Sun","Mon", "Tue"...]; 
    

    you would have received the day's name.