Functions and arrays source code

Lesson Details:
October 21, 2020


I: Introduction

If you want to become a web developer, you should take some courses on JavaScript. You can start with the free courses provided by Google.

Google offers free online courses on Web Development for beginners. The course is called “Get started with coding”, and it helps you to learn basic JavaScript. Google helps you also to learn HTML and CSS. To access the course, you need to sign up at Google Code University. After you have signed up, you will receive an email allowing you to access the courses.

The course is divided into modules, where each module contains videos or exercises. There are written instructions on how to complete the exercises, but if you fail to complete them, there are videos that explain how to complete the exercises. In addition, you can also ask instructors questions via chat if you get stuck with any of the exercises.

II: Body

In this section of your article, include code snippets from the lessons. Remember to add a description of what the code does in the paragraph above the code snippet. This will aid your reader in understanding what the code does.

Functions and arrays source code

Here is a function which returns a random number between 1 and 10:

function getRandomNumber(){ return Math.floor(Math.random() * 10); }

This function returns a random number between 1 and 10:

function getRandomNumber(){ return Math.floor(Math.random() * 10); } console.log(getRandomNumber()); //returns a random number between 1 and 10

In JavaScript, you can define a function inside another function. Here is an example where we define a function within another function:

function getRandomNumber(){ return Math.floor(Math.random() * 10); } function getRandomNumberTwo(){ return getRandomNumber(); } console.log(getRandomNumberTwo()); //returns a random number between 1 and 10

In JavaScript, you can also define an array using square brackets []. An array can contain different types of data such as numbers, strings, and objects (which we will cover in later lessons). To access individual elements in an array, you can use square brackets []. Here is an example:

var animal = [ 'dog', 'cat', 'horse' ]; console.log(animal[0]); //prints dog console.log(animal[1]); //prints cat console.log(animal[2]); //prints horse console.log(animal[3]); //undefined (not defined) console.log(animal[4]); //undefined (not defined) console.log(animal[5]); //undefined (not defined) console.log(animal[6]); //undefined (not defined) console.log(animal[7]); //undefined (not defined)

We can write an array using commas:

var animal = [ 'dog', 'cat', 'horse' ]; //same as: var animal = ['dog', 'cat', 'horse']; console.log(animal[0]); //prints dog console.log(animal[1]); //prints cat console.log(animal[2]); //prints horse console.log(animal[3]); //undefined (not defined) console.log(animal[4]); //undefined (not defined) console.log(animal[5]); //undefined (not defined) console.log(animal[6]); //undefined (not defined) console.log(animal[7]); //undefined (not defined)

You cannot assign values to undefined variables:

var animal = [ 'dog', 'cat', 'horse' ]; console.log(animal[4]); //undefined (not defined) console.log(animal[5]); //undefined (not defined) console.log(animal[6]); //undefined (not defined) console.log(animal[7]); //undefined (not defined)

loader
Course content