Here is a bunch of questions related to JavaScript arrays. It helps a developer to test his/her knowledge about JavaScript arrays.
Question 1:
Which of the following is / are valid statement(s) to declare an array?
A. const arr = [2, 4, 6];
B. const arr = new Array(2, 4, 6);
C. const arr = Array(2, 4, 6);
A and B
Only A
B and C
A, B and C
Question 2:
What is the output?
const arr = [2, , , 4];
console.log(arr.length);
5
4
2
SyntaxError: Invalid operator ,
Question 3:
What is the output?
const arr = [, , ,];
console.log(arr.length);
3
2
4
1
Question 4:
What is the output?
const arr = Array(3);
console.log(arr);
undefined
[3]
[undefined, undefined, undefined]
None of these
Question 5:
What is the output?
const arr = ["Mercedes", "BMW", "Audi"];
console.log(arr[1]);
"BMW"
"Mercedes"
"Audi"
undefined
Question 6:
What is the output?
const arr = ["Mercedes", "BMW", "Porsche"];
console.log(arr[1000]);
undefined
ReferenceError: Array out of bound exception
null
""
Question 7:
What is the output?
const arr = ["Apple", "Banana", "Orange"];
console.log(arr["2"]);
undefined
"Orange"
TypeError: Invalid array index
"Banana"
Question 8:
In what all ways, we can check if a variable arr
contains an array in JavaScript?
A. Array.isArray(arr) === true
B. arr.constructor.name === "Array"
C. typeof arr === "Array"
A and B
Only A
B and C
A, B and C
Question 9:
What is the output?
const arr1 = [1, 3, 5];
const arr2 = [2, 4, 6];
arr1.concat(arr2);
console.log(arr1);
[1, 2, 3, 4, 5, 6]
[2, 4, 6]
[1, 3, 5, 2, 4, 6]
[1, 3, 5]
Question 10:
const arr1 = ["Apple", "Banana"];
const arr2 = arr1.concat();
arr2[0] = "Pineapple";
console.log(arr1);
["Apple", "Banana"]
["Pineapple", "Banana"]
SyntaxError: missing argument for concat()
["Apple", "Banana", "Pineapple"]
Question 11:
What is the output?
const arr = [2, 4, 6];
const result = arr.every((ele) => ele * 2);
console.log(result);
true
[4, 8, 12]
false
[2, 4, 6]
Question 12:
What is the output?
const arr = [1, 3, 5, 7];
console.log(arr.fill(6, 1, 3));
[1, 6, 6, 7]
[1, 6, 6, 6]
[6, 1, 3, 7]
[6, 1, 3, 6]