JavaScript Array Methods For Absolute Beginners

Subscribe

There are JavaScript methods that you can take advantage of when you need to. We like to propose applications where you can use such methods. This helps you to understand them better. In this lesson today we are going to use a JavaScript method of push to show a random number.

Set The JavaScript Array

Lets set up the JavaScript array. We are going to use an example of the Power Ball Lottery. This consists of 6 numbers total. The first 5 numbers are 1 through 69. However the last number is 1 through 26. We are going to set up the first 5 numbers for our array.

var lucky = ["11", "25", "33", "42", "57"];

Generate a Random Number Math Function In JavaScript

To generate a random number from 1 to 26 we will use this JavaScript function stored in a variable called rand.

var rand = Math.floor(Math.random() * 26) + 1;

Use Push() Method To Add Final Number

Use the push method to add the final number to the array. This will be a number auto generated 1-26. Here is the syntax.

lucky.push(rand);

Try It Here

Join Method With JavaScript Arrays

You can use the join method to input something between each data item in your array.

var fruits = ["Tom", "Dick", "Harry"];
var x = document.getElementById("foo")
x.innerHTML = fruits.join(" / ");

See Join Method Here

Pop Method in JavaScript

Use the pop method when you want to remove the last item in the array.

See Pop Method Here

Return Items Popped Out

There is also a way to return the item that was popped out.

See Pop Method Here

Complete Set Of JavaScript Methods

Next Lesson

JavaScript Array Methods For Absolute Beginners was last modified: October 29th, 2021 by Maximus Mccullough

Summary
JavaScript Array Methods For Absolute Beginners
Article Name
JavaScript Array Methods For Absolute Beginners
Description
There are JavaScript methods that you can take advantage of when you need to. We like to like to propose applications where you can use such methods. This helps you to understand them better. In this lesson today we are going to use a JavaScript method of push to show a random number.
Author
Publisher
A1WEBSITEPRO LLC
Logo
javascript-methods-for-absolute-beginners

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.