DONATE  
MENU
AJAX
HTML
JAVASCRIPT
JSON

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Arrays » Accessing Arrays
Share Tweet WhatsApp

Accessing Arrays

In this page, you will learn how to access the various values of an array. Since arrays contain multiple values, using the variable upon which they were created or the array value itself in whatever operation will mean using all the values in it altogether which may be in an unorganized manner as a string separated with a comma(,).
<script>
var myArray = [1, "Hello World", true];
alert(myArray);
</script>

Copy PRACTICE IT

So, then, how do we access the individual values of an array? Like, if you have a number in an array, how do you access that number for whatever use? Index Number We access arrays using what is called an array "index number". An array's index number is a number starting from zero(0) which denotes the position of a value in an array. In JavaScript programming, the first value of an array has an index number of zero (0), the second value is one (1) and so on and so forth. So, while we as humans may start counting the values of an array from 1, JavaScript starts from 0 meaning the first value is located at position 0, which is then its index number. Having understood what an index number is, we can therefore access the value of an array by specifying the index number in brackets ([ ]) just in front of the array's name.
myArray[0] //First value
myArray[1] //Second value
EXAMPLE
<script>
var myArray = new Array("Stack", "Beginner", "Tutorials");

alert(myArray[0]);
</script>

Copy PRACTICE IT

If the last index number is 2 and you try to access any index number above that, the value that will be return is undefined. In strict programming languages like Java, such can result to errors and even failure to execute a program.
<script>
var myArray = new Array("Stack", "Beginner", "Tutorials");

alert(myArray[3]);
</script>

Copy PRACTICE IT

In the above example, the value will be undefined because there is no value for index number 3. The index ended at 2. SUMMARY
  1. When you use an array value or variable in any JavaScript operation, all the values in it would form a string joined together with a comma(,).
  2. To get an individual value contained in an array, we use its index number.
  3. The index number references the position of a value in an array. It starts from 0 meaning the first value of an array would have an index number of 0.
  4. We therefore put the index number of a value we want in brackets ([ ]) and in front of the array name. For example, myArray[1] to obtain the second value in the array named myArray.
Take a short quiz on Accessing Arrays.
Ask a question or tell us if Accessing Arrays is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode