使用 let 关键字声明一个名为 elementAtIndexFive 的变量。此变量的值应该是 numArray 数组中索引为 5 的元素。这是正确的吗?
const numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// TODO: declare the variable elementAtIndexFive and assign it
//the value of the element with index number 5 in the array numArray
let elementAtIndexFive = [1, 2, 3, 4, 5, 6];
// This will print the value of variable elementAtIndexFive to the console
console.log('The array element with index 5 is: ', elementAtIndexFive);
答案 0 :(得分:1)
JavaScript arrays 是零索引的。数组的第一个元素的索引为 0,最后一个元素的索引值等于数组的长度属性值减去 1。
因此您可以使用方括号表示法通过索引访问数组元素,它将返回存储在该索引处的值。
例如numArray[5]
const numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// TODO: declare the variable elementAtIndexFive and assign it
//the value of the element with index number 5 in the array numArray
let elementAtIndexFive = numArray[5];
// This will print the value of variable elementAtIndexFive to the console
console.log('The array element with index 5 is: ', elementAtIndexFive);
答案 1 :(得分:0)
let elementAtIndexFive = numArray[5];
const numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let elementAtIndexFive = numArray[5];
console.log('The array element with index 5 is: ', elementAtIndexFive);
您需要 numArray 中的第五个元素并将其分配给 elementAtIndexFive
答案 2 :(得分:-1)
是的,它会按预期打印,因为 let 关键字仅描述块存储中的变量;您的输出流和变量声明位于同一范围内。请记住,您不能再次重新声明。