JavaScript Arrays

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 is the purpose of the const keyword when declaring an array?

The const keyword defines a constant reference to an array, meaning that the array itself cannot be reassigned to a different array. However, the elements within the array can still be modified.

Which of the following methods can be used to add a new element to the end of an array?

  • `.shift()`
  • `.pop()`
  • `.unshift()`
  • `.push()` (correct)

What does the typeof operator return when applied to a JavaScript array, and why?

The typeof operator returns 'object' because a JavaScript array is technically an object. JavaScript arrays are complex data structures that inherit properties and methods from its object prototype.

Which method can be used to remove elements from an array while avoiding 'undefined holes' in the array?

<p><code>.splice()</code> (A)</p> Signup and view all the answers

The slice() method modifies the original array by removing elements.

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

What is the general purpose of the flat() method in JavaScript?

<p>The <code>flat()</code> method is used to convert a multi-dimensional array into a one-dimensional array by concatenating sub-array elements to a specified depth.</p> Signup and view all the answers

Explain the difference between the indexOf() and lastIndexOf() methods in JavaScript arrays.

<p>The <code>indexOf()</code> method locates the first occurrence of a specified element within an array, returning its index. Conversely, the <code>lastIndexOf()</code> method finds the last occurrence of the element and returns its index.</p> Signup and view all the answers

What is the purpose of the find() method in JavaScript arrays?

<p>The <code>find()</code> method iterates through an array, searching for the first element that satisfies a provided test function and returns the value of that element. Otherwise, it returns <code>undefined</code>.</p> Signup and view all the answers

Which of the following loop structures iterates through the properties of an object?

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

How does the JavaScript for/of loop differ from the for/in loop?

<p>The <code>for/of</code> loop iterates through the actual values of an iterable object, such as an array, while the <code>for/in</code> loop iterates through the properties (keys) of an object.</p> Signup and view all the answers

What is the fundamental difference between a while loop and a do/while loop?

<p>A <code>while</code> loop checks the condition before executing the loop body, meaning it might not execute at all if the condition is initially false. A <code>do/while</code> loop, however, executes the loop body at least once before checking the condition.</p> Signup and view all the answers

What is a regular expression in JavaScript?

<p>A regular expression is a sequence of characters that forms a search pattern. It allows for sophisticated text matching, searching, and replacement operations.</p> Signup and view all the answers

What are metacharacters in regular expressions, and what purpose do they serve? Give an example.

<p>Metacharacters are special characters that have a specific meaning in regular expressions, extending their functionality beyond basic text matching. For instance, <code>\d</code> represents any digit from 0 to 9. This allows for searching for patterns based on the presence of digits or specific characters, making regular expressions even more versatile.</p> Signup and view all the answers

Flashcards

What is an array?

A data structure in JavaScript that stores a collection of elements, ordered and indexed.

What does 'const' do when declaring an array?

Declaring an array using the const keyword creates a constant reference to the array. This means you can't reassign the variable to point to a different array, but you can modify the elements within the existing array.

How do you access an element in an array?

You can access specific elements within an array using their index, which starts from 0 for the first element.

What is the 'length' property of an array?

The length property of an array tells you how many elements are in the array.

Signup and view all the flashcards

What does the toString() method do for an array?

The toString() method converts an array into a comma-separated string.

Signup and view all the flashcards

What does the join() method do for an array?

The join() method provides more flexibility in converting an array to a string. You can specify a separator to separate the elements (e.g., space, hyphen, etc.)

Signup and view all the flashcards

How do you add an element to the end of an array?

The push() method adds an element to the end of an array.

Signup and view all the flashcards

How do you remove the last element from an array?

The pop() method removes the last element from an array and returns it.

Signup and view all the flashcards

How do you add an element to the beginning of an array?

The unshift() method adds an element to the beginning of an array.

Signup and view all the flashcards

How do you remove the first element from an array?

The shift() method removes the first element from an array and returns it.

Signup and view all the flashcards

What does the splice() method do?

The splice() method allows you to insert or remove elements at a specific position within an array.

Signup and view all the flashcards

What does the slice() method do?

The slice() method creates a new array that contains a portion of the original array, starting from the specified index and ending at the specified index.

Signup and view all the flashcards

What does the flat() method do?

The flat() method creates a new array by recursively flattening sub-arrays into a single-level array.

Signup and view all the flashcards

What does the indexOf() method do?

The indexOf() method finds the first occurrence of a specific element in an array and returns its index. If the element is not found, it returns -1.

Signup and view all the flashcards

What does the lastIndexOf() method do?

The lastIndexOf() method finds the last occurrence of a specific element in an array and returns its index. If the element is not found, it returns -1.

Signup and view all the flashcards

What does the find() method do?

The find() method searches for the first element in an array that satisfies a provided condition (a function) and returns that element. If no matching element is found, it returns undefined.

Signup and view all the flashcards

What does the findIndex() method do?

The findIndex() method searches for the first element in an array that satisfies a provided condition (a function) and returns its index. If no matching element is found, it returns -1.

Signup and view all the flashcards

What does the sort() method do?

The sort() method sorts the elements of an array in ascending order by default. It sorts as strings by default.

Signup and view all the flashcards

What is a compare function used for?

A function used within the sort() method to define how elements should be compared. It takes two arguments (a and b) representing elements from the array and returns a negative number if a is less than b, a positive number if a is greater than b, and zero if they are equal. Determines sorting order.

Signup and view all the flashcards

What does the reverse() method do?

The reverse() orders the elements of an array in descending order.

Signup and view all the flashcards

What does the forEach() method do?

The forEach() method executes a given function for each element in an array.

Signup and view all the flashcards

What is a for loop used for?

Looping through all elements within an array (e.g., to access each element for processing).

Signup and view all the flashcards

What is a for...in loop used for?

A loop that iterates over the properties of an object. Typically used in conjunction with a for loop.

Signup and view all the flashcards

What is a for...of loop used for?

A JavaScript loop that iterates over the elements (values) of an iterable object, such as an array or a string.

Signup and view all the flashcards

What is a while loop used for?

Allows you to execute a block of code repeatedly as long as a specified condition is true.

Signup and view all the flashcards

What is a do...while loop used for?

Ensures that a block of code is executed at least once, and then continues to execute it repeatedly as long as a specified condition is true.

Signup and view all the flashcards

What is a regular expression?

A sequence of characters that creates a search pattern for matching text and performing operations like text search or replace. It's often used with regular expressions.

Signup and view all the flashcards

What is a metacharacter in a regular expression?

A special character in a regular expression that has a specific meaning.

Signup and view all the flashcards

Study Notes

JavaScript Arrays

  • Arrays are ordered collections of values

  • Arrays can contain multiple data types (numbers, strings, etc)

  • Arrays are zero-indexed meaning the first element is at index zero

  • An array can span multiple lines as shown in the example code

  • Defining Arrays

    • Using square brackets: const cars = ["Opel", "BMW", "Mercedes"];
    • Using the new Array() constructor: const cars2 = new Array("Saab", "Volvo", "BMW");
    • This is equivalent to the above syntax
    • Arrays defined with const are not constant; they cannot be reassigned
  • Accessing Array Elements

    • Access elements using their index within the square brackets: cars[0] (returns "Opel")
    • To access the entire array, use document.getElementById("demo").innerHTML = cars;
  • Converting Arrays to Strings:

    • Using toString(): converts each element to a string and joins them with commas. cars.toString() would return "Opel,BMW,Mercedes"
  • Using join() : separates the array elements with a specified character: cars.join(" * ") would return "Opel * BMW * Mercedes"

  • Array Length:

    • Use .length to get the number of elements in the array: cars.length would return 3
  • Looping on Arrays:

    • Using a for loop: for (let i = 0; i < cars.length; i++) {text += cars[i];}
    • Using forEach(): A more concise approach for iterating through arrays.
    • cars.forEach(car => text+=car);
  • Checking if an item is in an array

    • Using indexOf(): Returns the index of the first occurrence of an element in the array.

      • Returns -1 if not found.
    • Using lastIndexOf(): Returns the index of the last occurrence of an element in the array. -Returns -1 if not found.

  • Arrays of objects

    • Arrays can contain objects as elements
    • Objects are collections of key-value pairs with labels
  • Methods for manipulating arrays:

    • push(): appends an element to the end.
    • pop(): removes the last element.
    • shift(): removes the first element.
    • unshift(): adds an element to the beginning.
    • concat(): creates a new array by merging existing arrays.
    • splice(): adds/removes elements from an array at a specific index (important to understand the parameters for new element addition/removal).
      • splice(index, amountToRemove, ...newElements)
    • slice(): creates a new array containing a portion of the source array.
    • flat(): creates a new array by flattening a nested array to a specified depth
  • Finding elements in Arrays:

    • find(): returns the first element matching a condition
    • findIndex(): returns the index of the first element matching a condition
  • Sorting Arrays:

    • sort(): sorts elements alphabetically, may need a compare function for numerical sorting
    • reverse(): reverses the order of elements
  • Using Math.min() and Math.max() on Arrays:

    • Math.min.apply(null, arr): Finds the smallest element in an array.
    • Math.max.apply(null, arr): Finds the largest element in an array.

JavaScript Loops

  • for loop: Repeats a block of code a specified number of times.

    • for (let i = 0; i < 10; i++) { //code }
  • for...in loop: Iterates over the properties of an object.

    • for (let key in myObj) { //code }
  • for...of loop: Iterates over the values of an iterable object.

    • for (const value of myArr) { //code }
  • while loop: Repeats a block of code as long as a condition is true.

    • while (condition) { //code }
  • do...while loop: Similar to while but executes the block of code at least once.

  • do { //some code } while(condition);

Regular Expressions

  • A sequence of characters that forms a pattern for searching or replacing text
  • Use /pattern/modifiers
  • Modifiers (e.g., i for case-insensitive) adjust how patterns match.
  • Key functions for working with Regex:
    • search(): to check if a string matches a pattern. -Returns the start index if a match is found. Returns -1 otherwise.
    • replace(): to replace parts of strings that match patterns.

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

JavaScript Arrays
3 questions

JavaScript Arrays

LoyalIllumination avatar
LoyalIllumination
JavaScript Arrays Properties and Methods
10 questions
JavaScript Arrays and Methods
5 questions
Use Quizgecko on...
Browser
Browser