0
0
Javascriptprogramming~20 mins

Array length property in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Array Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code?
Consider the following JavaScript code. What will be logged to the console?
Javascript
const arr = [1, 2, 3, 4];
arr.length = 2;
console.log(arr.length, arr);
A4 [1, 2, 3, 4]
B2 [1, 2, 3, 4]
C4 [1, 2]
D2 [1, 2]
Attempts:
2 left
πŸ’‘ Hint
Changing the length property can truncate the array.
❓ Predict Output
intermediate
2:00remaining
What is the output of this code?
What will this code print to the console?
Javascript
const arr = [];
arr[3] = 'hello';
console.log(arr.length);
A3
Bundefined
C4
D0
Attempts:
2 left
πŸ’‘ Hint
Array length is one more than the highest index.
❓ Predict Output
advanced
2:00remaining
What is the output of this code?
What will this code output?
Javascript
const arr = [1, 2, 3];
arr.length = 5;
console.log(arr[3], arr[4], arr.length);
Aundefined undefined 5
B3 5 5
Cundefined undefined 3
D3 denifednu denifednu
Attempts:
2 left
πŸ’‘ Hint
Increasing length adds empty slots, but does not add values.
❓ Predict Output
advanced
2:00remaining
What error does this code raise?
What happens when this code runs?
Javascript
const arr = [1, 2, 3];
Object.defineProperty(arr, 'length', {writable: false});
arr.length = 1;
console.log(arr.length);
Aundefined
BTypeError
C1
D3
Attempts:
2 left
πŸ’‘ Hint
Trying to change a non-writable property causes an error.
🧠 Conceptual
expert
2:00remaining
How many elements does this array have?
Given this code, how many elements does the array have?
Javascript
const arr = [,,];
console.log(arr.length);
A2
B1
Cundefined
D0
Attempts:
2 left
πŸ’‘ Hint
Empty slots count towards length.