【问题标题】:Why the square brackets after a variable? [duplicate]为什么变量后面有方括号? [复制]
【发布时间】:2022-06-10 17:36:16
【问题描述】:

一个非常愚蠢的问题,但我刚刚开始学习 javascript,需要编写代码让计算机随机选择数组中的三个值中的一个,如下所示:

function computerPlay() {
    let choices = ['rock', 'paper', 'scissors']; 
    let computerChoice = Math.floor(Math.random() * choices.length); 
    return choices[computerChoice]; 
}

我的问题是为什么在返回选择[computerChoice] 期间需要方括号?它是如何工作的?这叫什么?

【问题讨论】:

标签: javascript square-bracket


猜你喜欢
  • 2021-03-24
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-08-14
相关资源
最近更新 更多