css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              Nothin to see here folks! Check the console. 
This pen is a collection of the code examples found in this post: 
<a href="https://codepen.io/DevEarley/post/spread-and-rest-but-don-t-get-too-comfortable">https://codepen.io/DevEarley/post/spread-and-rest-but-don-t-get-too-comfortable</a>
            
          
!
            
              let shallowCopy_example = (function(){
	console.log("**************** Shallow Copy Example ****************");
	let someChild = {children:[{id:1},{id:2}]}
	let someObject = {prop1:true,prop2:false,child:someChild};
	let shallowCopy = {...someObject}; 
	someChild.children[0].id = 3;
	console.log("someObject === shallowCopy: "+(someObject === shallowCopy));
	console.log("Child Affected: "+(shallowCopy.child.children[0].id === 3));
})();

let deepCopy_example = (function(){
	console.log("**************** Deep Copy Example ****************");
	function deepCopy(obj){
		return JSON.parse(JSON.stringify(obj));
	}
	let someChild = {children:[{id:1},{id:2}]}
	let someObject = {prop1:true,prop2:false,child:someChild};
	let deepCopy = deepCopy(someObject); 	
	someChild.children[0].id = 3;
	console.log("someObject === deepCopy: "+(someObject === deepCopy));
	console.log("Child Affected: "+(deepCopy.child.children[0].id === 3));
})();

let rest_example = (function(){
	console.log("**************** Rest Example ****************");
	let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
	console.log("x: "+x)
	console.log("y: "+y)
	console.log("z: "+JSON.stringify(z))
})();

let spread_example_I = (function(){
	console.log("**************** Spread Example I ****************");
	let x = 1;
	let y = 2;
	let z = {a:3,b:4};
	let n = { x, y, ...z };
	console.log("n: "+JSON.stringify(n))
})();

let spread_example_II = (function(){
	console.log("**************** Spread Example II ****************");
	let x = {prop1:true, prop2:false};
	let y = {prop3:1, prop4:2}
	let n = {...x, ...y}
	console.log("n: "+JSON.stringify(n))
})();

let function_definitions_example = (function(){
	console.log("************* Function Definitions Example I *************");
	function sum (...numbers) {
		let result = 0;
		numbers.forEach(function(n){
			result += n;
		});
		return result;
	};
	var sumOf3Numbers = sum(2,2,2);
	var sumOf5Numbers = sum(2,3,2,2,1);
	console.log("Sum of 2,2,2: "+ sumOf3Numbers);
	console.log("Sum of 2,3,2,2,1: "+ sumOf5Numbers);	

	var arrayOfValues = [1,2,3];
	var sumOfArray = sum(...arrayOfValues);
	console.log("Sum of [1,2,3]: "+ sumOfArray);
})();

let function_definitions_example = (function(){
	console.log("************* Function Definitions Example II *************");
	function filter(type, ...items) {  
		return items.filter(item => typeof item === type);
	}
	var boolValues = filter('boolean', true, 0, false);	
	var numberValues = filter('number', false, 4, 'Welcome', 7);
	console.log("Boolean values: "+boolValues);
	console.log("Number values: "+numberValues);
})();

let iteration_example_I = (function(){
	console.log("************* Iteration Example I *************");
	let countries = ['India', 'USA'];
	let otherCountries = ['China', 'Japan'];
	countries.push(...otherCountries);
	console.log(countries);
	// India, USA, China, Japan
})();

let iteration_example_II = (function(){
	console.log("************* Iteration Example II *************");
	let countries = ['India', 'USA'];
	let otherCountries = ['China', 'Japan'];
	countries.unshift(...otherCountries);
	console.log(countries);
	// China, Japan, India, USA
})();

let iteration_example_III = (function(){
	console.log("************* Math Functions Example I *************");
	Array.prototype.min = function() {
		var r = this[0];
		this.forEach(function(v,i,a){if (v<r) r=v;});
		return r;
	};
	let someArrayOfNumbers = [9,7,8,3,4,1,2,3,5,6];
	let smallestNumber = someArrayOfNumbers.min();
	console.log("Min: "+smallestNumber);	
})();


let iteration_example_IV = (function(){
	console.log("************* Math Functions Example II *************");
	let someArrayOfNumbers = [9,7,8,3,4,1,2,3,5,6];
	let smallestNumber =Math.min(...someArrayOfNumbers);
	console.log("Min: "+smallestNumber);	
	let wrongSmallestNumber =Math.min(someArrayOfNumbers);
	console.log("NaN Min: "+wrongSmallestNumber);	
})();


let iteration_example_V = (function(){
	console.log("************* Iteration Example III *************");
	let someArrayOfNumbers = [9,7,8,3,4,1,2,3,5,6];
	console.log(...someArrayOfNumbers);
})();

let excersise_I = (function(){
	console.log("************* Excersise I *************");
	let someArrayOfNumbers = [9,7,8,3,4,1,2,3,5,6];
	let expectedOutcome = [9,7,8,3,4,1,2,3,5,6,9,7,8,3,4,1,2,3,5,6];

	let duplicate = (...onlyOneParam) => //rest
	{
		return [...onlyOneParam, ...onlyOneParam]; //spread 
	}

	var outcome = duplicate(...someArrayOfNumbers);//spread
	console.log("Outcome: "+JSON.stringify(outcome))
	console.log("Valid: "+(JSON.stringify(outcome) == JSON.stringify(expectedOutcome)))

	var outcome2 = duplicate(someArrayOfNumbers);//one param
	console.log("Outcome2: "+JSON.stringify(outcome2))
	console.log("Valid2: "+(JSON.stringify(outcome2) == JSON.stringify(expectedOutcome)))

	let duplicate2 = (onlyOneParam) => //one param
	{
		return [...onlyOneParam, ...onlyOneParam]; //spread
	}

	var outcome3 = duplicate2(someArrayOfNumbers); // one param
	console.log("Outcome3: "+JSON.stringify(outcome3))
	console.log("Valid3: "+(JSON.stringify(outcome3) == JSON.stringify(expectedOutcome)))
})();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console