Created
April 5, 2021 13:24
-
-
Save vishalkukreja/62a7013b2fe2349ebccac0ff7fbc997a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cityNames = ['Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore']; | |
console.log(...cityNames); // Mumbai Delhi Pune Ahmadabad Indore | |
var numbers = [22, 11, 98, 43, 87, 56, 45, 01, 24]; | |
// Without spread operator | |
console.log("Smallest no.:",Math.min(numbers)); // Smallest no.: NaN | |
// With spread operator | |
console.log("Smallest no.:",Math.min(...numbers)); // Smallest no.: 1 | |
// copy an array | |
var arr = [10, 20 ,30 ]; | |
var newArr = [...arr]; | |
console.log(newArr); //[ 10, 20, 30 ] | |
// concat an array | |
var city1 = ['Mumbai', 'Pune', 'Delhi']; | |
var city2 = ['Surat', 'Ahmadabad', 'Kutchh']; | |
newCity = [...city1, ...city2, 'Thane']; //same as city1 = city1.concat(city2) | |
console.log(newCity); //[ 'Mumbai', 'Pune', 'Delhi', 'Surat', 'Ahmadabad', 'Kutchh', 'Thane' ] | |
//clone object | |
let cityObj = { city: 'Mumbai', state: 'MH' }; | |
let clonedObj = { ...cityObj }; | |
console.log(clonedObj); //{ city: 'Mumbai', state: 'MH' } | |
let cityObj1 = { city: 'Mumbai', state: 'MH' }; | |
let cityObj2 = { town: 'Unr', state: 'MH' }; | |
let newClonedObj = { ...cityObj1, ...cityObj2 }; | |
console.log(newClonedObj); //{ city: 'Mumbai', state: 'MH', town: 'Unr' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment