//-------------------------------------------
/REST operator
function sum(...numbers) {
console.log("Performing addition of", numbers);
let sumofnums = 0;
numbers.forEach(function (item) {
sumofnums += item;
});
return sumofnums;
}
console.log("Sum of 3 numbers: ", sum(1, 2, 3));
console.log("Sum of 5 numbers: ", sum(1, 2, 3, 4, 5));
function createTeam(teamName, captain, ...members) {
return {
name: teamName,
captain: captain,
members: members,
};
}
const team = createTeam("Avengers", "Iron Man", "Thor", "Hulk", "Black Widow");
console.log(team);
const colors = ["red", "green", "blue", "yellow", "purple"];
const [primaryColor, secondaryColor, ...otherColors] = colors;
console.log("Primary color:", primaryColor);
console.log("Secondary color", secondaryColor);
console.log("Other colors:", otherColors);
//REST operator with object de-structuring
const person = {
name: "Sarah",
age: 28,
city: "Chicago",
job: "Developer",
hobbies: ["reading", "hiking"],
};
const { name, age, ...otherDetails } = person;
console.log("Name", name);
console.log("age", age);
console.log("Other details", otherDetails);
//---------------------------------------------
//Spread operator
//expanding arrays
const numbers = [1, 2, 3, 4, 5];
console.log(...numbers);
//combining arrays
const fruits = ["apple", "orange"];
const vegetables = ["carror", "potato"];
const produce = [...fruits, ...vegetables];
console.log(produce);
//copying arrays
const copyNumbers = [...numbers, 6, 7, 8];
console.log(copyNumbers);
//copying objects
const person = { name: "Alex", age: 30 };
const personCopy = { ...person };
console.log(personCopy);
//adding properties while copying
const personCopyAndAdd = { ...person, hobby: "reading", age: 31 };
console.log(personCopyAndAdd);
//---------------------------------------------------
//Truthy and Falsy
// the && operator returns the first falsy operand, or the last truthy if all operands are truthy.
console.log(true && "Hello");
console.log(true && "hi" && "Hello"); //returning the last truthy operand
console.log(false && "Hello");
console.log(false && 0 && "Hello"); // returning the first falsy operand
console.log("" && "Hello");
console.log(0 && "Hello");
//Javascript has exactly six falsy values, everything else is truthy
//false //boolean
//0 //the number 0
//'' //Empty string ('' or "")
//null //absence of any value
//undefined //uninitialized variable
//NaN //not a number
//The || operator returns the first truthy operand or the last falsy operand if all the operands are falsy
console.log(true || "Hello");
console.log(false || "Hello");
console.log(0 || "" || null || "Default");
console.log(0 || "" || null || undefined);
//-----------------------------------------------------