Day 4 of 100 Days of Code

nkemdev

Nkem

Posted on February 21, 2022

Day 4 of 100 Days of Code

Today I learnt about calling a function within a function and arrays. An array is like a container that can hold more than one value at the same time. It could be an ordered list of items, composite data types or complex data types. This differentiates it from a variable that can just hold a value at a time.

For example

let card = [“firstNumber” ,”secondNumber”]
console.log(card)
Enter fullscreen mode Exit fullscreen mode

The output will be
[“firstNumber”,”secondNumber”]

To access an array, you do this
card[0].

Accessing an array starts at zero not one.
Arrays have their own built in methods. Methods can be described as functions on an object. The commonly known ones are;
• Length
• Concat
• Push
• Pop

Length
This is used to determine the number of elements in an array
card.length // This will return two as there are two elements in the card array.

Concat is used to combine two arrays and return a combined array.

cars =[‘ferrari’, ‘Lamboghini’]
card.concat(cars)
Enter fullscreen mode Exit fullscreen mode

This will return [“firstNumber”, “secondNumber”,’ferrari’,’Lamboghini’]

Push
This is to add an element to the array

newCard =”ThirdNumber” 
card.push(newCard) 
Enter fullscreen mode Exit fullscreen mode

This will add the new element “thirdNumber” into the array.

Pop
This is to remove an element from the array
card.pop(newCard)
This removes the 'newCard' element from the array.

💖 💪 🙅 🚩
nkemdev
Nkem

Posted on February 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related