JavaScript Variables and Array methods

Rushabhsoni
4 min readJul 18, 2021

--

Variables are the storage space required for us in the programming to store numbers, strings, etc. Variables are the key components in any programming language.

There are three ways you can declare a variable in JavaScript:

Using the “var” keyword:

When you declare a variable using the “var” keyword, you don’t necessarily need to initialize it you can just declare it and use it. Re-Declaration and re-updation of the variable are allowed. Example:

Redeclaration and updation of a variable using “var”

Using the “let” keyword:

By using the “let” keyword, like “var”, you don’t necessarily need to initialize earlier, you can simply declare it, redeclaration is not allowed, but you can update it.

Using the “const” keyword:

This keyword basically states that once you entered the value it can never be changed, you can’t redeclare it or update it, even compulsorily need to initialize at the time of declaration.

JS Array and its Methods:

Arrays are storehouse to store values.JS arrays are global objects just like lists in Python. The length of the JS arrays are not fixed, they can be changed at any point with non-contiguous memory locations.

There are several methods to perform traversal and mutation on JS.

push method:

To insert the elements in the array at the end.

const vehicles= [“Car”, “Van”, “Truck”, “Bike”];
vehicles.push(“Scooter”); // Adds “Scooter” to vehicles

O/P:- [“Car”, “Van”, “Truck”, “Bike”,” Scooter”]

pop method

The pop() method removes the last element from an array

const vehicles= [“Car”, “Van”, “Truck”, “Bike”];
vehicles.pop(); // Removes “Bike” from vehicles

O/P:- [“Car”, “Van”, “Truck”]

Shift method

Removes the element at index ‘0’.

const vehicles= [“Car”, “Van”, “Truck”, “Bike”];
vehicles.shift(); // Removes “Car” from vehicles

O/P:- [ “Van”, “Truck”, “Bike”]

Unshift method

Insert an element at index ‘0’.

const vehicles= [“Car”, “Van”, “Truck”, “Bike”];
vehicles.unshift(“Scooter”); // Removes “Car” from vehicles

O/P:- [ “Scooter”, “Car”, “Van”, “Truck”, “Bike”]

splice method

The method can be used to add new items to an array.

const vehicles= [“Car”, “Van”, “Truck”, “Bike”];
vehicles.splice(2,0,“Scooter”); 1st param : start at index 2,2nd param : remove zero elements and add values.

O/P:- [“Car”, “Van”, “Scooter”, “Truck”, “Bike”]

concat method

Merges arrays by creating a new array.

const twoWheeler= [ “Bike” “Scooter”];
const fourWheeler= [“Truck”, “Car”, “Van”];
const vehicles= twoWheeler.concat(fourWheeler);

O/P:- [“Bike”, “Scooter”, “Truck”, “Car”, “Van”]

slice method

The method is used to remove a section of the array. It creates a new array. It does not remove any elements from the source array.

var vehicles = [“Car”, “Van”, “Scooter”, “Truck”, “Bike”];

var new_vehicles= vehicles.slice(1, 4);

O/P:-[ “Van”, “Scooter”,“Truck”]

map method

Map method allows us to access array and manipulate it however we want.

const a = [1,2,3,4,5]

const d = a.map(function(item){ return item*item })

console.log(d)

O/P:-[1,4,9,16,25]

filter method

FIlter creates a new array with some specific conditions.

var vehicles = [“Car”, “Van”, “Scooter”,“Truck”, “Bike”];

var filtered_vehicle = vehicles.filter(function(item){return item.length ≤3})

O/P:- [“Car”, “Van”]

forEach method

The method calls a function once for each element in an array, in order.

let sum = 0;
const numbers = [10,2,5,6];
numbers.forEach(myFunction);

function myFunction(item) {
sum += item;
}

O/P:-23

find method

This method returns the value of the element in the array that passes the condition.

var vehicles = [“Car”, “Van”, “Scooter”,“Truck”, “Bike”]

value = vehicles.find(checkType(vehicle){

return vehicle == “Truck”;

})

O/P:-Truck

toString method

The method returns a string with all array values separated by commas.

const vehicles= [“Car”, “Van”, “Scooter”, “Truck”, “Bike”]
let text = vehicles.toString();

O/P:-Car, Van, Scooter, Truck, Bike

--

--

Rushabhsoni

Tech-implementer,Blogger,Web-Developer,Machine Learning Enthusiast