1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 5.00 out of 5)

JavaScript Array Methods For Absolute Beginners

javascript-methods-for-absolute-beginners

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 Poped Out *

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

See Pop Method Here

Complete Set Of JavaScript Methods

Next Lesson

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
A1WEBSITEPRO LLC

Related Post

JavaScript Array Methods For Absolute Beginners was last modified: June 28th, 2017 by Maximus Mccullough

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider buying me a drink.

Drinks

Leave a Reply

Your email address will not be published. Required fields are marked *