Array & Math - Brief Explanation

Array & Math - Brief Explanation

Welcome to my blog. Arrays are the most essential concept of JavaScript. We will learn all important concepts on array and its pre-build methods.

ยท

5 min read

Array

What is an Array?

An Array object is a collection of multiple items in a single variable name.

const names = ['John', 'Steve', 'Paul'];
console.log(names) // ['John', 'Steve', 'Paul']
console.log(names.length) // 3
console.log(names[2]) // Paul

To check the length of an array we have to use array.length property of an array.

Create an Array

1.Using an array literal The easiest way to create an array is by using an array literal []. For example,

const array = ["Str1", "Str2"];

2.Using the new keyword not recommended You can also create an array using JavaScript's new keyword.

const array = new Array("Str1", "Str2");

Some example are

// empty array
const myList = [ ];

// array of numbers
const numberArray = [ 1,2,3,4];

// array of strings
const stringArray = [ 'Russia', 'India'];

// array with mixed data types
const newData = ['heavy', 'exercise', 1, true];

Access Elements of an Array

We all can access elements of an array using there indexes. Indexes in array starts from (0,1,2,3,4,5,6,...)

For example

const array = ['h', 'e', 'l', 'l', 'o'];

// first element
console.log(array[0]);  // "h"

// second element
console.log(array[1]); // "e"

.

.

and so on.

Some Useful Array Method

MethodsWhat they do
concat()joins two or more arrays and returns a result
indexOf()searches an element of an array and returns its position
find()returns the first value of an array element that passes a test
findIndex()returns the first index of an array element that passes a test
forEach()calls a function for each element
includes()checks if an array contains a specified element
push()aads a new element to the end of an array and returns the new length of an array
unshift()adds a new element to the beginning of an array and returns the new length of an array
pop()removes the last element of an array and returns the removed element
shift()removes the first element of an array and returns the removed element
sort()sorts the elements alphabetically in strings and in ascending order
slice()selects the part of an array and returns the new array
splice()removes or replaces existing elements and/or adds new elements
reverse()reverse method is used to read the array from last index or reverse the array element

Some examples of arrays and their methods

let array = ['string1', 'string2', 'string3', 'string4']

// display array
console.log(array); // [ 'string1', 'string2', 'string3', 'string4' ]

// push an element at end of  array
array.push('string5')
console.log(array); // [ 'string1', 'string2', 'string3', 'string4', 'string5' ]

// pop an element from end in  array
array.pop()
console.log(array); // [ 'string1', 'string2', 'string3', 'string4' ]

// slice
let array2 = array.slice(1, 3)
console.log(array2); // [ 'string2', 'string3' ]

//splice
let array3 = [1, 2, 3, 4, 5, 6]
array3.splice(2, 2, 'string1', 'string2', 'string3')
console.log(array3); // [ 1, 'string1', 'string2', 'string3', 4, 5, 6 ]

//concat
let value1 = [1, 2, 3]
let value2 = [4, 5, 6]
console.log(value1.concat(value2)); // [ 1, 2, 3, 4, 5, 6 ]

// copyWithin: copy value of an array inside another array
// Syntax: array.copyWithin(target_index, start, end)
let arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(arr1.copyWithin(1, 5, 8));
/*
[
    1, 6, 7, 8, 5,
    6, 7, 8, 9
]
*/


let animals = ['tiger', 'lion', 'cheetah', 'cat', 'tiger']

//includes
console.log(animals.includes('tiger'));
// true. includes return true or false based on value available in array or not.

// indexOf
console.log(animals.indexOf('tiger')); // 0, return the index of item in array

// lasIIndexOf
console.log(animals.lastIndexOf('tiger')); // 4, return the index of item in array

//map
animals.map(animal => console.log(animal))

/*
tiger
lion
cheetah
cat
tiger
*/

//reverse
console.log(animals.reverse()) // [ 'tiger', 'cat', 'cheetah', 'lion', 'tiger' ]

// shift
console.log(animals.shift()); // tiger
console.log(animals); // [ 'cat', 'cheetah', 'lion', 'tiger' ] - first element of an array has been removed

let ball = ['ball4', 'ball3', 'ball2', 'ball1'];

// sort
console.log(ball.sort()); // [ 'ball1', 'ball2', 'ball3', 'ball4' ] - value is sorted in ascending order

// toString
console.log(ball.toString()); //changing your array to normal format

// unshift
let number = [1, 2, 3, 4, 5, 6, 7]
console.log(number.unshift(0)); // 8 
console.log(number); // [0, 1, 2, 3, 4, 5, 6, 7]

// split
let str = 'javascript'
let result1 = str.split()
console.log(result1); // [ 'javascript' ]
let result2 = str.split('') // [ 'j', 'a', 'v', 'a', 's', 'c', 'r', 'i', 'p', 't' ]

Array is an also object.

Math

The JavaScript Math object allows you to perform mathematical tasks on numbers.

Some examples of Math object

// Math
const PI = Math.PI // gives value of PI
console.log(PI); // 3.141592653589793

console.log(Math.round(PI)); // 3 - 
console.log(Math.floor(PI)); // 3 - floor is the lowest value b/w ranges
console.log(Math.ceil(PI)); // 4 - ceiling is the heoght value b/w ranges

// Minimum and Maximum
console.log(Math.min(5, 558, 454, 561, 89, 74, 123)); // 5
console.log(Math.max(5, 558, 454, 561, 89, 74, 123)); // 561

//random
console.log(Math.random()); // 0.08997023075023769 - ite gives values b/w 0 to 1
console.log(Math.random() * 11); //5.140040920021246 

//absolute
console.log(Math.abs(-10)); // 10 - it will give posiitve value alway

// square root
console.log(Math.sqrt(49)); // 7

// power
console.log(Math.pow(3, 2)); // 9

// trigonometric - JavaScript support all trignometric formulas.
console.log(Math.cos(60)); // -0.9524129804151563
console.log(Math.log(2)); // 0.6931471805599453
console.log(Math.log10(2)); // 0.3010299956639812

Math is an object. It provides so many inbuilt methods which we is handy and easy to use in our code.

End

I hope this has been a useful read.

I recommend using this guide as a reference.

Good luck! Share love ๐Ÿ’–

Special Thanks to ๐Ÿ’– Hitesh Choudhary Sir for making us grow every day.

Bye!!!

ย