Monday, August 23, 2021

Create an Array in Javascript Basic Guideline.

Array in Javascript : Using array we can store multiple value in one variable in Javascript like other programming language

If we store array in const variable we can change its value by assign new value because array is not primitive value so it can be modified. But we can not change entire array.

// Create an array in Javascript

const friends = ['a', 'b', 'c', 'd', 'e'];
console.log(friends);

// Other way to create array...

const years = new Array(1991, 1992, 1993, 1994, 1995);
console.log(years);
// Get Array length..

console.log(friends.length);

//Get last array element value..
console.log(friends[friends.length-1]);

// Putting deffrent Value in Array and See on Console.

const friends = ['a','b','c','d'];
const firstName = 'Anil';
const Details = [firstName,'Singh',2021-1990,friends];
console.log(Details);
// Array Excercise Javascript..

const calcAge = function (birthYear) {
return 2021 - birthYear;
}
const years = [2002, 1995, 1985, 1975, 1973, 1978];
const age1 = calcAge(years[0]);
const age2 = calcAge(years[1]);
const age3 = calcAge(years[years.length - 1]);
console.log(age1, age2, age3);

const age = [ calcAge(years[0]), calcAge(years[1]), calcAge(years[years.length - 1]), ];
console.log(age);

0 comments

Post a Comment