Sitemap
Soha - JavaScript Developer

Soha - JavaScript Developer

WordPress Expert | Python - Django Projects | Your Coding Ally

JavaScript Arrays

3 min readDec 10, 2022

Let's learn about arrays and do some practice!

“Arrays help maintain large sets of data under a single variable name to avoid confusion that can occur when using several variables.”

Working with Arrays brings lot of fun. You can create many applications just by using an array.

In JavaScript, an array is an object that can hold a jumble of different data types.

JavaScript Arrays are:

  • resizable.
  • mutable. // Their values can be changed.
  • zero-indexed. // The first element of an array is at index 0

To find the length of an array, you can use the length property.

To add and delete elements from an array, you can use push(), pop(), or slice().

So, to create an array in javascript, there are two ways:

  1. Array literals // [ ]
  2. new keyword // new Array()
// array literals method
const arr = [1,2,3,'banana','apple', true];

// new keyword
const arr = new Array(1,2,3,'banana','apple', false);

Now, let's practice arrays by creating an audio player that plays random audio every time when we click on the play button.

Warning: It's not like an actual audio player, just a simple version for practice.

For fun 😁

Folder Structure

Firstly, I downloaded some random audio from the internet and then created a folder named “audio” and copy the audio files there.

Get Soha - JavaScript Developer’s stories in your inbox

Join Medium for free to get updates from this writer.

In the main part, I created an array named “audioFiles ”, and push the audio file name as a string. Then, simply target the <audio> “id” using “document.getElementById” and store it in the “audioSrc” variable.


const audioFiles = ['audio/fluteSound.mp3','audio/drumSound.mp3','audio/randomSound.mp3'];

const audioSrc = document.getElementById('audioSrc');
const link = document.getElementById('link');



let playAudio = () => {

audioSrc.src = audioFiles[Math.floor(Math.random()* audioFiles.length)];
link.innerHTML = audioSrc.src
}

I created a function called “playAudio”, then through “audioSrc”, you can simply target the audio src.

Like this: audioSrc.src

To set the src, just write the array name and the index number, because as you know each time we have to target a single and random element from the array so for this, I used :

“Math.floor(Math.random()* audioFiles.length)”

Assign it with the src.

audioSrc.src = audioFiles[Math.floor(Math.random()* audioFiles.length)];

HTML Part:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Audio Player</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
.center {
margin: auto;
width: 60%;
border: 3px solid #40094b;
padding: 10px;
margin-top:200px;
text-align: center;
}
#audio{
color: white;
background-color: #40094b;
border-radius: 6px;
border: none;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
#link{
color:#40094b;
font-weight: bold;
}

</style>
</head>
<body>

<div class="center">
<h3>Random Audio | Player </h3>
<audio id="audioSrc" controls autoplay>

</audio>
<br>
<p><b>Audio Link: </b><span id="link"></span></p>
<br>
<button type="button" id="audio" onclick="playAudio()">Play</button>
</div>

<script src="js/index.js"></script>
</body>
</html>

And here is the final result.😍

Press enter or click to view image in full size
Output
Press enter or click to view image in full size
Press enter or click to view image in full size

That's it. Hope you understand the concept.

And if you like it, give me feedback.

--

--

Soha - JavaScript Developer
Soha - JavaScript Developer

Written by Soha - JavaScript Developer

WordPress Expert | Python - Django Projects | Your Coding Ally