Cornell Herzog

Cornell Herzog

1606792860

JavaScript ES6 - Spread Operator - Advanced

Combine two arrays or objects in javascript? Append an object in existing object? This video will cover whole about spread operator in javascript and its various technical for manipulating data like object, arrays etc.

#javascript #es6 #web-development #programming #developer

What is GEEK

Buddha Community

JavaScript ES6 - Spread Operator - Advanced

Santosh J

1622036598

JavaScript compound assignment operators

JavaScript is unarguablly one of the most common things you’ll learn when you start programming for the web. Here’s a small post on JavaScript compound assignment operators and how we use them.

The compound assignment operators consist of a binary operator and the simple assignment operator.

The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).

It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.

They are:

  • +=
  • -+
  • *=
  • /=

You can also check my video tutorial compound assignment operators.

Let’s consider an example. Suppose price = 5 and we want to add ten more to it.

var price = 5;
price = price + 10;

We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.

price += 5;

Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.

Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.

price -= 5;
console.log(price);

Lets multiply price and show it.

price *=5;
console.log(price);

and finally we will divide it.

price /=5;
console.log(price);

If you have any doubts, comment below.

#javascript #javascript compound assignment operators #javascript binary operators #javascript simple assignment operator #doers javascript

Eldora  Bradtke

Eldora Bradtke

1594059180

Rest Parameter and Spread Operator

The Rest Parameter

A Rest Parameter is used as the last argument of a function declaration. It is enabling the user to specify any number of parameters.

function sumUp(...nums) {
	  return nums.reduce((acc, cur) => acc = Number(acc + cur), 0);
	}

	console.log(sumUp(1, 2, 3));
	// -> 6

A function can also have common parameters as long as the Rest Parameter is the last one.

	function myHero(name, level, ...abilities) {
	  return `My hero ${name} on level ${level} has the following abilities: ${abilities.join(", ") || 'none'}.`;
	}

	console.log(myHero('Arthas', 55, 'Attack', 'Blizzard'));
	// -> My hero Arthas on level 55 has the following abilities: Attack,Blizzard.

	console.log(myHero('Thrall', 1));
	// -> My hero Thrall on level 1 has the following abilities: none.

Rest Parameter vs. Arguments

Arguments is an Array-like Object; we cannot directly apply Array-functions to it. We have to make use of the Spread Operator to unfold the Arguments Object in an Array first. For instance: In the last example, we could apply the join method directly to our abilities parameter.

function sumUp() {
	  return [...arguments].reduce((acc, cur) => acc = Number(acc + cur), 0);
	}

	console.log(sumUp(1, 2, 3));
	// -> 6

#spread-operator #es6 #rest-parameter #learn-javascript #javascript-basics #javascript

Tanya  Shields

Tanya Shields

1593530460

JavaScript: Spread Operator and Rest Parameter (ES6)

The spread operator and rest parameter are two other powerful features introduced in ECMAScript 2015 and they allow us to write more concise and flexible code like most of the other ES6 features do. The several different ways you can use spread and rest syntax will be covered in this article.


Spread Operator

The spread operator allows us to expand the collected elements of an iterable data type like strings, arrays, or objects into individual elements in places. It helps to create a list of elements from a list by spreading out the values of an iterable where zero or more arguments (function calls) or elements (array literals) or key-value pairs (object literals) are expected.

1. In Function Calls:

The spread operator can be used to take elements of an array and pass them as a list of arguments into a function. Let’s take a look at how it works:

The elements of an array are passed to a function as arguments

The function printColors takes in three parameters and we want to pass three arguments stored in colors array. When we call the function, the spread operator unpacks the passed array of arguments, ‘red’‘blue’, and ‘yellow’.

#es6 #javascript #rest-parameter #programming #spread-operator

Cornell Herzog

Cornell Herzog

1606792860

JavaScript ES6 - Spread Operator - Advanced

Combine two arrays or objects in javascript? Append an object in existing object? This video will cover whole about spread operator in javascript and its various technical for manipulating data like object, arrays etc.

#javascript #es6 #web-development #programming #developer

Macey  Kling

Macey Kling

1598690640

Why I Love The Spread Operator

Over the last few years, JavaScript has seen massive improvements to its syntax and functionality with the addition of a bunch new features.

One of the most useful of those new features is the spread operator (aka the … operator)

Spread is super helpful, but its syntax is not particularly meaningful upon first glance. It doesn’t tell you much as to what it does. Fortunately, once you learn the three main uses for Spread, it will become one of your favourite tools in the JS toolbox!

_According to MDN: _“Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.”

There are three places you can use spread:

  1. Function calls
  2. Array literals
  3. Object literals

Spread & Functions

When passing arguments to a function, We can use spread to expand an iterable element. Rather than having to pass each argument individually, We can “Spread” out each item as its argument.

**Math.min**is a function which accepts any number of arguments and returns the minimum value. If we wanted to find the minimum value of an array, It doesn’t work to pass the entire array in:

const temperatures = [76,72,68,79,54,65];

Math.min(temperatures)
//Result
▶ NaN

Instead, we can use spread to expand our array, Passing in each element as an argument:

const temperatures = [76,72,68,79,54,65];
Math.min(...temperatures) 
//the same as Math.min(76,72,68,79,54,65)
//Result
▶ 54

Here’s one more example of using console.log. If we pass an entire array to console.log

const tvShows = ["Six Feet Under", “Chernobyl”, “Black Mirror”, "Fleabag " ]

console.log(tvShows);

#programming #javascript #spread-operator #web-development #es6