DEV Community


Data Structure and Algorithm Basics part[0]

adroitcoder profile image John Samuel Obinna ・5 min read

Before you jump into Data Structure & Algorithm #mypersonalnotebook I would assume you have basic knowledge of the following:

  • Conditional
  • Repetition Construct (forloop & while loops)
  • Functions
  • Variable Scope
  • Recursion
  • Declaring Classes

Just basic stuff a JavaScript programmer will know.


  • All output will be displayed on the Console.
  • Data Structure & Algorithm will be broken into parts.
  • If you don't have a basic knowledge above-listed, I will recommend you grab a copy of Eloquent Javascript
  • All codes used here are available on my Github repository.


Every programming language includes some form of array.
Arrays are the most common data structure in computer programming.

Array in JavaScript

Arrays in javascript are linear collection of elements where each element can be access via an index (numeric indexing).

How to Create Arrays in Javascript

The most recommended way of creating arrays in javascript is by Declaring a variable using the computed members access [] operator.

var myArray = [];

When you create an array like this, you will have an array with a length value of 0. Let's play around to be sure.

var myArray =  [];

We can also create an array using the constructed pattern.

var myArray =  new Array();
  • Arrays in javascript are collections of anything(functions,objects,primitives e.t.c)
  • Counting begins at index 0 not 1

Accessing Array Elements

Array Elements can be accessed using the [] operator.

var names =  ["Somto", "Ezeh",];
console.log(names[0] + ' ' names[1])// Somto Ezeh

Writing Array Elements

Assigning data to the array we use []

var numbers =  [];
for(var i= 0; i < 10; i++){
    numbers[i] = i+1;

Array from strings

Invoking the split() function on a string breaks up that string into separate parts and produces an array from that individual string.

var string = "I Love javascript ";
var makeArray = string.split(' ');

Accesor Functions

With the javascript accessor function, we can access an element of an array and return some value.


The indexOf function loops through an array to see if the parameter passed to it, is found inside the target array and return the position.

 var names = ["John","Samuel","Samuel","Obinna"];
 var position = names.indexOf("Samuel");

The indexOf() function returns the position of the first element that matches the argument or returns -1, if not found.

The lastIndexOf function loops through an array to see if the parameter passed to it, is found inside the target array and returns the last position matched or returns -1, if not found.

 var names = ["John","Samuel","Samuel","Obinna"];
 var position = names.lastIndexOf("Samuel");

toString() and join() returns a string representation of an array separated with commas.

  var myArray  = ["How to use","Accesor","Functions"];
  var makeString = myArray.join()
  console.log(makeString);//How to use,Accesor,Functions

To get rid of the commas, you pass in an empty quote to the join() function.

  var myArray  = ["How to use","Accesor","Functions"];
  var makeString = myArray.join(" ")
  console.log(makeString);//How to use Accesor Functions

The concat() and splice() function produces a new array from an existing array.
The concat() function combines more arrays and produces a new array, and the splice() function produces a new array from a subset of a target array.


var firstArray  = ["Princess", "Precious", "Patience", "Pessy"];
var secondArray = ["Adesunloye","Adedeji","Adetoro"];
var joinAll = firstArray.concat(secondArray);
console.log(joinAll);//["Princess", "Precious", "Patience", "Pessy","Adesunloye","Adedeji","Adetoro"]


var names  = ["Princess", "Precious", "Patience", "Pessy"];
var newArray = names.splice(2,3);

Mutator Functions


The pop() function removes an element from the end of an Array and returns the value.

var numbers   = [1,2,3,4,5,6];
var remove  = numbers.pop();
console.log(remove);// 6

Adding an element to an array
There are two ways to add an element to an array.
They are push() and unshift() but we are going to talk about the push() function first.


The push() function adds an element to the end of an array.

var array = [1,2,3,4,5,6,7,8];
console.log(array) // 1,2,3,4,5,6,7,8,9

The unshift() function adds an element to the beginning of an array.

var array = [2,3,4]; 

The shift() function removes an element from the beginning of an Array and returns the value

var array = ["I", "LOVE", "ARRAYS"];
var firstElementRemoved = array.shift(); 
console.log(firstElementRemoved);// I

//You can also decide to discard the return value 

var array = ["I", "LOVE", "ARRAYS"];
console.log(array);//["LOVE", "ARRAYS"]

The splice() function adds or removes an element from the middle of an array.
The following arguments are needed to add or remove an element(s) from the middle of an array.

  • The starting index (where you want to begin adding elements).
  • The number of elements to remove (0 when you are adding elements).
  • The elements you want to add to the array.
var array = [10,20,30,70,80,90];
var newArray = [40,50,60]; 
console.log(array); //10,20,30,[40,50,60],70,80,90

Elements passed into the splice() function can be a list and not necessarily a collection of arrays.

var array = [10,20,30,70,80,90];
console.log(array); //10,20,30,40,50,60,70,80,90

Here is an example of using splice() to remove elements from an array:

var intro = ["Hello", "Callme", "John", "Samuel"];
var removeintro = intro.splice(1,2);

The reverse() function reverses the order of the elements of an array.

var array = [1,2,3,4,5,6,7,8,9];
var reverseArray = array.reverse();

The sort() function sorts the elements of an array lexicographically: for more information about lexicographically

var names = ["Williams","Joe","John","Samuel","Somto","Ezeh"]; 
console.log(names);// Ezeh,Joe,John,Samuel,Somto,Williams

Yay! All is working Fine, now let's see how the sort functions works with numbers.

var numbers = [1,2,3,4,50,60,70,80,5,6,7];

Not what we expected. Don't worry we can fix this by passing a function to the sort() function.

function compare(num1, num2) {
   return num1 - num2;
var nums = [1,2,3,4,50,60,70,80,5,6,7]; 
var sorted = nums.sort(compare);
console.log(sorted)// 1,2,3,4,5,6,7,50,60,70,80

YES! It works.

Datastructure & Algorithm Basics part[1] will be available soon...

Discussion (0)

Editor guide