DONATE  
MENU
JAVASCRIPT
HTML
AJAX
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 » Introduction » JavaScript Arrays
Share Tweet WhatsApp

JavaScript Arrays

An array is a special data type in JavaScript that is used in holding multiple values of whatever data type on a variable. Arrays can contain multiple values and these values may be numbers, strings, booleans, other arrays, objects and functions. EXAMPLE
var myArray = [1, "Hello World", true, [1, 2, 3], {age:4}, function myFunc(){}];
Above is an example of an array containing values of other data type. Arrays are useful when you want to create a variable that should contain multiple values e.g a person's details.
var person = ["John Schwartz", 43, "5.4m", "Web Developer"];
Above, just one variable contained multiple values which are a person's name, age, height, skills. Syntax Arrays can be created explicitly or implicitly/using the Array object prototype. Both does same thing which is to create an array.
//EXPLICITLY
var myArray = [1, "Hello World", true, [1, 2, 3], {age:4}, function myFunc(){}]; //Literal Way

//IMPLICITLY
var myArray = new Array(1, "Hello World", true, [1, 2, 3], {age:4}, function myFunc(){}); //Object Way
Accessing Arrays Each of the value of an array can be access using an index number. An index number is a number that represents the position of a particular value in an array. The first value of an array has an index number of 0 and so on.
<script>
var myArray = [1, "Hello World", true, [1, 2, 3], {age:4}, function myFunc(){}];

alert(myArray[1]); //alerting second value
</script>

Copy PRACTICE IT

Arrays' typeof() When you use the typeof function on an array, it returns an object. This is because arrays are actually objects.
<script>
var myArray = [1, "Hello World", true, [1, 2, 3], {age:4}, function myFunc(){}];

alert(typeof(myArray));
</script>

Copy PRACTICE IT

But then, how can we distinguish an array from other sort of objects? We can create a custom function to do this.
<script>
//THE FUNCTION
function isArray(value){
if (value !== undefined){
if (value.constructor.toString().indexOf("Array") > -1){
return "array";
}
else
{
return false;
}
}
else
{
return false;
}
}

//THE ARRAY
var myArray = [1, "Hello World", true, [1, 2, 3], {age:4}, function myFunc(){}];

//CHECK ARRAY
alert(isArray(myArray));

alert(isArray("Hello World"));

</script>

Copy PRACTICE IT

EXPLAINED Above, we created a function to check if a value is an array or not. Whenever the function is invoke, an argument which may be a value of any data type is passed to it, it checks to determine if it is an array or not. The first thing it does is that, since all values or variables in JavaScript have an object and an object constructor function used in creating the object, it accesses that object constructor (value.constructor) and converts it to a string (value.constructor.toString()) and find the position of the word "Array" in the string (value.constructor.toString().indexOf("Array")). If the value is an array, it will find the word "Array" at a position greater than -1. Precisely position 9. If it finds the position, it returns "array" else it returns "false". You will learn more on arrays in the arrays chapter. SUMMARY
  1. An array is a JavaScript data type that can contain multiple values.
  2. Arrays are accessed using an index number.
  3. Using the typeof() operator on an array will return object because arrays are objects.
  4. We can create a function to distinguish arrays from other forms of objects.
Take a short quiz on JavaScript Arrays.
Ask a question or tell us if JavaScript 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