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 (B)</p> Signup and view all the answers

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

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

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

<p>0 (D)</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 (D)</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 (D)</p> Signup and view all the answers

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

<p>Removes the last element from the array (A)</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() (C)</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 (D)</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 (C)</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 (C)</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 (D)</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. (B)</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); (A), let arrayCopy3 = array.slice(); (C)</p> Signup and view all the answers

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

<p>reduce() (D)</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. (D)</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. (D)</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. (C)</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. (B)</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] (C)</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. (D)</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 (C)</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. (B)</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 (C)</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 (A)</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() (C)</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 (C)</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 (A)</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; (C)</p> Signup and view all the answers

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

<p>Undefined (C)</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. (B)</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 (D)</p> Signup and view all the answers

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

<p>1 (C)</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 (B)</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 (A)</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() (A)</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 (A)</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 (B)</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 (B)</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 (C)</p> Signup and view all the answers

Flashcards

Array

A data structure storing a group of elements.

Array Element

A single value within an array.

Array Index

A number representing an element's position in an array (starting at 0).

Array Length

The total number of elements in an array.

Signup and view all the flashcards

Array Method: toString()

Converts an array to a string representation.

Signup and view all the flashcards

Array Method: join()

Combines array elements into a single string, using a specified separator.

Signup and view all the flashcards

Array Method: pop()

Removes and returns the last element of an array.

Signup and view all the flashcards

Array Method: push()

Adds one or more elements to the end of an array and returns the new length.

Signup and view all the flashcards

Array pop()

Removes the last element from an array and returns it.

Signup and view all the flashcards

Array push()

Adds one or more elements to the end of an array and returns the new array's length.

Signup and view all the flashcards

Array shift()

Removes the first element from an array and returns it. Changes the array's length.

Signup and view all the flashcards

Array unshift()

Adds one or more elements to the beginning of an array and returns the new length.

Signup and view all the flashcards

Array concat()

Merges two or more arrays into a new array. Does not modify the original arrays.

Signup and view all the flashcards

Array filter()

Used to create a new array with all elements that pass a test. The test is provided as a function.

Signup and view all the flashcards

Array find()

Returns the value of the first element in an array that satisfies a provided testing function.

Signup and view all the flashcards

Array includes()

Checks if an array contains a specified element.

Signup and view all the flashcards

Array copy (bad)

Assigning an array variable to another does not copy the array’s values; only the reference. Changing one variable affects the other.

Signup and view all the flashcards

Array copy (good)

Use spread syntax (...) to create a true copy of an array, which creates a new array with the values of the original.

Signup and view all the flashcards

Rest parameters

Allows a function to accept any number of arguments as an array.

Signup and view all the flashcards

Arrow functions

Shorthand syntax for creating functions. Concise and often more readable. Arrow function vs standard function.

Signup and view all the flashcards

Array immutability

Methods like 'filter', 'map', 'find', etc., return new arrays based on the original array. They do not modify the original array itself.

Signup and view all the flashcards

Array

A data structure holding multiple values.

Signup and view all the flashcards

Array element

A single value inside an array.

Signup and view all the flashcards

Array Length

The total number of items in an array.

Signup and view all the flashcards

Array index

A number that tells the position of an element.

Signup and view all the flashcards

Array method: push()

Adds an element to the end of an array.

Signup and view all the flashcards

Array method: pop()

Removes the last element from an array.

Signup and view all the flashcards

Array method: toString()

Converts an array to a string.

Signup and view all the flashcards

Array method: join()

Joins array elements into one string.

Signup and view all the flashcards

Array pop()

Removes and returns the last element of an array.

Signup and view all the flashcards

Array push()

Adds one or more elements to the end of an array and returns the new length.

Signup and view all the flashcards

Array shift()

Removes and returns the first element from an array, changing the length of the array.

Signup and view all the flashcards

Array unshift()

Adds one or more elements to the beginning of an array and returns the new length.

Signup and view all the flashcards

Array concat()

Creates a new array by merging two or more arrays. Does not change the original arrays.

Signup and view all the flashcards

Array filter()

Creates a new array containing elements that pass a test implemented by a provided function.

Signup and view all the flashcards

Array copy (bad)

Assigning an array variable to another only copies the reference, not the values. Changes to one array affect the other.

Signup and view all the flashcards

Array copy (good)

Creates a new array with the values of an original using spread syntax (...).

Signup and view all the flashcards

Rest parameters

Allows a function to accept an arbitrary number of arguments and collect them into an array.

Signup and view all the flashcards

Arrow Functions (good practice)

Shorthand function syntax (more concise) that does not change scope of this in the function execution.

Signup and view all the flashcards

Array Copy vs Reference

Direct assignment copies the reference to the array, not the array itself. The spread syntax creates an independent copy.

Signup and view all the flashcards

Callback (in Array Methods)

A function that is passed as an argument to another function and is called inside that function

Signup and view all the flashcards

Callback element

An individual value in a callback function often related to an array element.

Signup and view all the flashcards

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 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