0
0
Javascriptprogramming~5 mins

Array creation in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the simplest way to create an empty array in JavaScript?
You can create an empty array using square brackets: []. This makes a new list that can hold items.
Click to reveal answer
beginner
How do you create an array with initial values 1, 2, and 3?
Use square brackets with the values inside separated by commas: [1, 2, 3]. This creates an array holding those numbers.
Click to reveal answer
intermediate
What does new Array(5) do in JavaScript?
It creates an array with 5 empty slots. The array has length 5 but no actual values set yet.
Click to reveal answer
intermediate
How can you create an array from a string like 'hello'?
Use Array.from('hello'). This makes an array of each character: ['h', 'e', 'l', 'l', 'o'].
Click to reveal answer
intermediate
What is the difference between [] and new Array()?
[] creates an empty array. new Array() also creates an empty array but can behave differently if you pass a number (it sets length).
Click to reveal answer
Which syntax creates an array with three elements: 10, 20, and 30?
AAll of the above
B[10, 20, 30]
CArray(10, 20, 30)
Dnew Array(10, 20, 30)
What does new Array(4) create?
AAn array with 4 undefined elements
BAn error
CAn array with one element: 4
DAn array with length 4 but empty slots
How to create an array from the string 'abc'?
AArray.from('abc')
B['abc']
Cnew Array('abc')
DString.toArray('abc')
Which is the correct way to create an empty array?
A[]
BBoth A and B
Cnew Array()
DArray.empty()
What happens if you do new Array(3, 4)?
ACreates an array with length 4
BCreates an array with length 3
CCreates an array with elements 3 and 4
DThrows an error
Explain how to create arrays in JavaScript and the difference between using [] and new Array().
Think about how length and elements are set.
You got /3 concepts.
    Describe how to convert a string into an array of characters in JavaScript.
    Consider how strings and arrays relate.
    You got /3 concepts.