6. JavaScript Array
42 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the pop() method do in an array?

  • Merges two arrays into one
  • Returns the last element removed from the array (correct)
  • Adds an element to the end of the array
  • Removes the first element from the array
  • What will the output of console.log(fruits.toString()) be after executing the following code: let fruits = ['Apple', 'Banana', 'Pear']; fruits.pop(); ?

  • 'Apple,Banana' (correct)
  • 'Apple,Banana,Pear'
  • 'Banana,Pear'
  • 'Apple,Pear'
  • What is the effect of using unshift() on an array?

  • It sorts the array in ascending order
  • It removes elements from the end of the array
  • It returns the new length of the array and modifies the existing array (correct)
  • It creates a copy of the array
  • Which of the following correctly describes the concat() method?

    <p>It returns a new array without altering the original arrays</p> Signup and view all the answers

    What will console.log(num.shift()) return given the array let num = [1, 2, 3];?

    <p>1</p> Signup and view all the answers

    What starting index number is used to access elements in an array?

    <p>0</p> Signup and view all the answers

    What does the length property of an array represent?

    <p>The total count of the elements in the array</p> Signup and view all the answers

    What will be the output of console.log(fruits.toString()) for the array let fruits = ['Apple', 'Banana', 'Pear']?

    <p>Apple,Banana,Pear</p> Signup and view all the answers

    What does the pop() method do in an array?

    <p>Removes the last element from the array</p> Signup and view all the answers

    If you want to concatenate all elements of an array into a string with a custom separator, which method would you use?

    <p>join()</p> Signup and view all the answers

    What will be the result of array = array + array when array is initialized as array = 1?

    <p>3</p> Signup and view all the answers

    Which of the following statements about the shift() and unshift() methods is correct?

    <p>shift() removes the first element, unshift() adds to the beginning</p> Signup and view all the answers

    Given the array let mix = [1, 'Apple', 22, 'Banana'], what does console.log(mix[mix.length-3]) output?

    <p>Apple</p> Signup and view all the answers

    What will the variable 'result1' contain after executing the filter function on the array 'words'?

    <p>Words with lengths greater than 6</p> Signup and view all the answers

    Which statement correctly describes why the 'fakeSheeps' variable equals 'sheeps'?

    <p>It assigns the same reference of the array.</p> Signup and view all the answers

    What is one way to create a proper copy of the array 'array'?

    <p>let arrayCopy2 = Array.from(array);</p> Signup and view all the answers

    Which of the following methods is NOT mentioned as an array method in the content?

    <p>reduce()</p> Signup and view all the answers

    How can the rest parameters be defined in a function?

    <p>By declaring a single parameter with a spread operator.</p> Signup and view all the answers

    What is the outcome of the expression 'sheeps === cloneSheeps'?

    <p>It will return false, indicating they are different objects.</p> Signup and view all the answers

    What does the map method do when it processes each element?

    <p>It returns a new array with the results of calling a provided function on every element.</p> Signup and view all the answers

    In the context of array copying, what is a drawback of the 'bad' copying method illustrated?

    <p>It copies the reference rather than the values.</p> Signup and view all the answers

    What is the resulting array after executing num.unshift(22, 33) on the array let num = [1, 2, 3];?

    <p>[22, 33, 2, 3]</p> Signup and view all the answers

    Which of the following statements about the concat() method is true?

    <p>concat() returns a new array containing merged elements.</p> Signup and view all the answers

    What will console.log(fruits.length) output after executing the following code: let fruits = ['Apple', 'Banana', 'Pear']; fruits.pop(); ?

    <p>2</p> Signup and view all the answers

    What does the filter() method do in the context of an array?

    <p>It creates a new array with elements that pass a specific test.</p> Signup and view all the answers

    Given the array let array = ['abc', true, 1, 99, false]; what will the following loop print out? for (let i = 0; i < array.length; i++) { console.log(array[i]); }

    <p>Each element of the array on a new line</p> Signup and view all the answers

    What will be the result of calling array.length when array has been populated with elements?

    <p>The total count of elements in the array</p> Signup and view all the answers

    Which method would you use to convert an array into a single string with a specified separator?

    <p>join()</p> Signup and view all the answers

    What does the pop() method return when applied to an array?

    <p>The last element of the array and modifies the array length</p> Signup and view all the answers

    What is the effect of using the push() method on an array?

    <p>It adds one or more elements to the end of the array</p> Signup and view all the answers

    In the array 'fruits = ["Apple", "Banana", "Pear"]', what will console.log(fruits.join(" *** ")) output?

    <p>&quot;Apple *** Banana *** Pear&quot;</p> Signup and view all the answers

    What is the initial value of array elements when declared with 'let arr = []'?

    <p>Undefined</p> Signup and view all the answers

    Which of the following statements correctly describes the toString() method?

    <p>It returns a string representation of the specified array and its elements.</p> Signup and view all the answers

    What will be contained in the 'result1' variable after executing the filter function on the array 'words'?

    <p>All words with more than 6 characters</p> Signup and view all the answers

    What will console.log(mix[mix.length-3]) return for mix = [1, 'Apple', 22, 'Banana']?

    <p>1</p> Signup and view all the answers

    What does the spread operator '...' do when used with the 'sheeps' array?

    <p>It creates a shallow copy of the array</p> Signup and view all the answers

    Which statement correctly describes the difference between 'fakeSheeps' and 'cloneSheeps' variables?

    <p>'cloneSheeps' points to the same memory space, 'fakeSheeps' does not</p> Signup and view all the answers

    Which method is used to check if all elements in an array meet a certain condition?

    <p>every()</p> Signup and view all the answers

    What mistake is made in the 'bad' copying practice illustrated in the content?

    <p>Creating a reference instead of a copy</p> Signup and view all the answers

    How is the 'map()' function used incorrectly in the 'bad' example?

    <p>The function's syntax does not use arrow functions</p> Signup and view all the answers

    What is the purpose of a callback function when using the 'filter()' method?

    <p>To determine if elements meet a certain condition</p> Signup and view all the answers

    What does the rest parameter syntax allow in a function definition?

    <p>To group multiple arguments into an array</p> Signup and view all the answers

    Study Notes

    Arrays

    • Arrays are data structures holding elements.
    • Array elements are numbered, starting from zero.
    • Access elements using their number in square brackets.
    • The total count of array elements is its length.
    • The toString() method returns a string representation of the array and its elements.
    • The join() method concatenates array elements, separated by commas or a specified separator string.

    Array Methods

    • pop(): Removes and returns the last element of an array. Changes the array's length.
    • push(): Adds one or more elements to the end of an array, and returns the new length.
    • shift(): Removes and returns the first element of an array. Changes the array's length.
    • unshift(): Adds one or more elements to the beginning of an array and returns the new length.
    • concat(): Merges two or more arrays into a new array, without altering the original arrays.
    • forEach(): Executes a provided function once for each array element.
    • map(): Creates a new array with the results of calling a provided function on every element.
    • filter(): Creates a new array with all elements that pass a provided test.

    Other Methods

    • find(), every(), some(), includes() - Additional array methods.

    Arrays Copy

    • Arrays in JavaScript are reference values, meaning when you copy an array using = you copy the reference, not the array contents. Modifying one copy will also modify the original.
    • To create a true copy, use spread syntax (...) or other methods that explicitly copy the array elements.

    Best Practices

    • Use of for...in and for...of loops are less efficient than forEach function.
    • Use rest parameters (...) to accept an indefinite number of arguments in a function as an array.
    • Spread syntax can expand iterables like arrays into their elements (for function calls or as new variables).

    Sorting Arrays

    • sort() may not sort in a way you expect it to work with numbers unless a comparison function is supplied specifically
    • Avoid using array.sort() with numbers if unexpected results are required with the elements of an array, or it is essential which sorting behavior to achieve.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    JS Masyvai PDF

    Description

    This quiz covers the basics of arrays, including their structure and key methods like pop(), push(), and concat(). Test your understanding of how to manipulate and access array elements effectively in programming. Ideal for students learning about data structures.

    More Like This

    Array Data Structures Quiz
    5 questions
    Arrays in Programming
    5 questions

    Arrays in Programming

    DivineZebra9695 avatar
    DivineZebra9695
    Computer Programming - Arrays and Strings
    24 questions
    Use Quizgecko on...
    Browser
    Browser