Podcast
Questions and Answers
What is the purpose of the const
keyword when declaring an array?
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?
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?
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?
Which method can be used to remove elements from an array while avoiding 'undefined holes' in the array?
The slice()
method modifies the original array by removing elements.
The slice()
method modifies the original array by removing elements.
What is the general purpose of the flat()
method in JavaScript?
What is the general purpose of the flat()
method in JavaScript?
Explain the difference between the indexOf()
and lastIndexOf()
methods in JavaScript arrays.
Explain the difference between the indexOf()
and lastIndexOf()
methods in JavaScript arrays.
What is the purpose of the find()
method in JavaScript arrays?
What is the purpose of the find()
method in JavaScript arrays?
Which of the following loop structures iterates through the properties of an object?
Which of the following loop structures iterates through the properties of an object?
How does the JavaScript for/of
loop differ from the for/in
loop?
How does the JavaScript for/of
loop differ from the for/in
loop?
What is the fundamental difference between a while
loop and a do/while
loop?
What is the fundamental difference between a while
loop and a do/while
loop?
What is a regular expression in JavaScript?
What is a regular expression in JavaScript?
What are metacharacters in regular expressions, and what purpose do they serve? Give an example.
What are metacharacters in regular expressions, and what purpose do they serve? Give an example.
Flashcards
What is an array?
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?
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?
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?
What is the 'length' property of an array?
Signup and view all the flashcards
What does the toString()
method do for an array?
What does the toString()
method do for an array?
Signup and view all the flashcards
What does the join()
method do for an array?
What does the join()
method do for an array?
Signup and view all the flashcards
How do you add an element to the end of an array?
How do you add an element to the end of an array?
Signup and view all the flashcards
How do you remove the last element from an array?
How do you remove the last element from an array?
Signup and view all the flashcards
How do you add an element to the beginning of an array?
How do you add an element to the beginning of an array?
Signup and view all the flashcards
How do you remove the first element from an array?
How do you remove the first element from an array?
Signup and view all the flashcards
What does the splice()
method do?
What does the splice()
method do?
Signup and view all the flashcards
What does the slice()
method do?
What does the slice()
method do?
Signup and view all the flashcards
What does the flat()
method do?
What does the flat()
method do?
Signup and view all the flashcards
What does the indexOf()
method do?
What does the indexOf()
method do?
Signup and view all the flashcards
What does the lastIndexOf()
method do?
What does the lastIndexOf()
method do?
Signup and view all the flashcards
What does the find()
method do?
What does the find()
method do?
Signup and view all the flashcards
What does the findIndex()
method do?
What does the findIndex()
method do?
Signup and view all the flashcards
What does the sort()
method do?
What does the sort()
method do?
Signup and view all the flashcards
What is a compare function used for?
What is a compare function used for?
Signup and view all the flashcards
What does the reverse()
method do?
What does the reverse()
method do?
Signup and view all the flashcards
What does the forEach()
method do?
What does the forEach()
method do?
Signup and view all the flashcards
What is a for loop used for?
What is a for loop used for?
Signup and view all the flashcards
What is a for...in loop used for?
What is a for...in loop used for?
Signup and view all the flashcards
What is a for...of loop used for?
What is a for...of loop used for?
Signup and view all the flashcards
What is a while loop used for?
What is a while loop used for?
Signup and view all the flashcards
What is a do...while loop used for?
What is a do...while loop used for?
Signup and view all the flashcards
What is a regular expression?
What is a regular expression?
Signup and view all the flashcards
What is a metacharacter in a regular expression?
What is a metacharacter in a regular expression?
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
- Using square brackets:
-
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;
- Access elements using their index within the square brackets:
-
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
-
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
- Use
-
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);
- Using a
-
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 conditionfindIndex()
: returns the index of the first element matching a condition
-
Sorting Arrays:
sort()
: sorts elements alphabetically, may need a compare function for numerical sortingreverse()
: 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 towhile
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.