javascript method what/why/when

by vaibhav2812

HTML

<script src="http://jsfiddle.net/vaibhav2812/ru0dpeyf/13/"></script>

JavaScript

const names = ['adam', 'bob', 'cathy'];
const countries = ['India', 'Pakistan', 'America', 'China', 'Bhutan']
let cities = [{
    name: 'Bangalore',
    population: 800000,
    state: 'Karnataka'
  },
  {
    name: 'Mumbai',
    population: 800000,
    state: 'Maharashtra'
  },
  {
    name: 'Pune',
    populaton: 800000,
    state: 'Maharashtra'
  },
  {
    name: 'Mysore',
    population: 800000,
    state: 'Karnataka'
  },
  {
    name: 'Chennai',
    population: 800000,
    state: 'Tamilnadu'
  },
  {
    name: 'Delhi',
    population: 800000,
    state: 'Tamilnadu'
  },
  {
    name: 'Kolkata',
    population: 800000,
    state: 'P.Bangal'
  }
];

const countryCapitalized = countries.map(country => country.toUpperCase());
console.log(countryCapitalized); // Countries  ["INDIA", "PAKISTAN", "AMERICA", "CHINA", "BHUTAN"]
console.log(countries); // countries is still ['India', 'Pakistan', 'America', 'China', 'Bhutan']

const changedPopulation = cities.map((city) => city.population = 1000000);
console.log(changedPopulation) //  array with [1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000]
console.log(cities);
/* but here get array of object with updated population value
0:{name: "Bangalore", population: 1000000, state: "Karnataka", country: "India"}
1:{name: "Mumbai", population: 1000000, state: "Maharashtra", country: "India"}
2:{name: "Pune", populaton: 800000, state: "Maharashtra", population: 1000000, country: "India"}
3:{name: "Mysore", population: 1000000, state: "Karnataka", country: "India"}
4:{name: "Chennai", population: 1000000, state: "Tamilnadu", country: "India"}
5:{name: "Delhi", population: 1000000, state: "Tamilnadu", country: "India"}
6:{name: "Kolkata", population: 1000000, state: "P.Bangal", country: "India"}
length:7
*/
const addCountry = cities.map((city) => city.country = 'India');
console.log(addCountry); // here you will get array with added values.
console.log(cities); // but here youw will array of object with added...