Introduction to Arrays & the Math Object
Master JavaScript Arrays and Math Objects Fundamentals
Core Concepts You'll Master
JavaScript Arrays
Learn to create, manipulate, and access array elements using zero-based indexing. Master essential array methods like push() and sort().
Math Object Methods
Discover random number generation and mathematical operations. Use Math.random() and Math.floor() for dynamic functionality.
Dynamic Content Display
Combine arrays with Math objects to create interactive web features that change content dynamically on page load.
Setup Requirements
Visual Studio Code or similar with folder support recommended
Located in Desktop > Class Files > yourname-JavaScript jQuery Class
This will be your main working file for the exercise
Chrome DevTools will be essential for testing and debugging
Now let's populate our array with actual data. Modify your code to include string values:
var myArray = ['Madrid', 'Paris', 'London'];
Notice how each string is wrapped in quotes and separated by commas—this is the proper syntax for array elements.
The Console displays undefined—this is completely normal and expected behavior.
In JavaScript, operations either return a value or return undefined. Array creation is a statement that performs an action but doesn't produce a return value, hence the undefined response. This distinction between statements and expressions is fundamental to understanding JavaScript's execution model.
myArray; and press Return (Mac) or Enter (Windows).["Madrid", "Paris", "London"] displayed in the Console, confirming our array was created successfully.To access specific array elements, we use bracket notation with the element's index position:
myArray[0];
Remember: JavaScript uses zero-based indexing, so the first element is always at position 0.
Execute this command to see "Madrid" returned.
To retrieve the third element, use index 2:
myArray[2];
Pro tip: Use your Up Arrow key to cycle through previous Console commands—a huge time-saver during development.
This returns "London", demonstrating how array indexing works in practice.
Essential Array Methods
push() Method
Adds new elements to the end of an array. Returns the new array length after addition.
length Property
Returns the number of elements in an array. Useful for dynamic operations and loops.
sort() Method
Arranges array elements in alphabetical order. Modifies the original array structure.
Use console.dir(myArray) to see an interactive list of all available array methods and properties. This is invaluable for exploring what operations you can perform on your arrays.
JavaScript arrays start counting from 0, not 1. This means the first item is at index 0, the second at index 1, and so on. This fundamental concept is crucial for accessing array elements correctly.
Creating Your First Array
Open Chrome Console
Press Cmd-Opt-J (Mac) or CTRL-Shift-J (Windows) to access the developer console
Declare Empty Array
Type 'var myArray = [];' - the square brackets denote an empty array structure
Add Array Values
Populate with strings: var myArray = ['Madrid', 'Paris', 'London']; separating each with commas
Test Array Access
Use myArray[0]; to get 'Madrid', myArray[2]; to get 'London' - practice zero-based indexing
Implementing Dynamic Testimonials
Create Script Section
Add script tags before closing body tag and declare quotesArray with placeholder values
Target HTML Element
Use document.getElementById('press-quote') to select the testimonial paragraph element
Modify Text Content
Change testimonial text using .textContent property and array index access
Math object contains constants like PI (uppercase) that never change, and methods like random() that perform calculations. Constants provide fixed mathematical values while methods execute operations.
Math Rounding Methods
| Feature | Math.floor() | Math.ceil() |
|---|---|---|
| Direction | Rounds Down | Rounds Up |
| Example: 3.78 | Returns 3 | Returns 4 |
| Use Case | Array indices | Capacity planning |
Using quotesArray.length instead of hard-coded numbers makes your code flexible. When you add or remove testimonials, the random selection automatically adjusts without code changes.
Random Selection Implementation
Generate Random Decimal
Math.random() produces a decimal between 0 and 1
Scale to Array Range
Multiply by quotesArray.length to get range 0 to array size
Convert to Integer
Wrap in Math.floor() to get whole number for valid array index
Apply to DOM
Use result as array index to select random testimonial
Math object contains constants like PI (uppercase) that never change, and methods like random() that perform calculations. Constants provide fixed mathematical values while methods execute operations.
Math Rounding Methods
| Feature | Math.floor() | Math.ceil() |
|---|---|---|
| Direction | Rounds Down | Rounds Up |
| Example: 3.78 | Returns 3 | Returns 4 |
| Use Case | Array indices | Capacity planning |
Using quotesArray.length instead of hard-coded numbers makes your code flexible. When you add or remove testimonials, the random selection automatically adjusts without code changes.
Random Selection Implementation
Generate Random Decimal
Math.random() produces a decimal between 0 and 1
Scale to Array Range
Multiply by quotesArray.length to get range 0 to array size
Convert to Integer
Wrap in Math.floor() to get whole number for valid array index
Apply to DOM
Use result as array index to select random testimonial
The split() method transforms a single string into an array by breaking it at specified characters. This enables precise text manipulation for formatting testimonials and attributions separately.
Advanced Text Formatting
Identify Split Character
Use emdash (—) to separate testimonial from attribution
Test Split Method
Use console.log() to verify split() creates proper array structure
Extract Testimonial Only
Access index [0] to get just the testimonial portion
Add Professional Quotes
Concatenate curly quotes for polished presentation
Key Takeaways
