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

What is GEEK

Buddha Community

JavaScript: Spread Operator and Rest Parameter (ES6)
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

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

Ethan Hughes

Ethan Hughes

1604859720

Quick tips: destructuring, rest parameter, and spread operator

Today I wanted to write a short post about some quick ES6 tips: destructuring, rest parameter, and spread operator . These three musketeers are present in all modern JS  frameworks and for a long time I was confused about these concepts until I had Vue _ and React _ on my programming routine. Hey ho, let’s go!

#javascript #es6 #es6-in-depth #web-development #javascript-tips

Wilford  Pagac

Wilford Pagac

1594289280

What is REST API? An Overview | Liquid Web

What is REST?

The REST acronym is defined as a “REpresentational State Transfer” and is designed to take advantage of existing HTTP protocols when used for Web APIs. It is very flexible in that it is not tied to resources or methods and has the ability to handle different calls and data formats. Because REST API is not constrained to an XML format like SOAP, it can return multiple other formats depending on what is needed. If a service adheres to this style, it is considered a “RESTful” application. REST allows components to access and manage functions within another application.

REST was initially defined in a dissertation by Roy Fielding’s twenty years ago. He proposed these standards as an alternative to SOAP (The Simple Object Access Protocol is a simple standard for accessing objects and exchanging structured messages within a distributed computing environment). REST (or RESTful) defines the general rules used to regulate the interactions between web apps utilizing the HTTP protocol for CRUD (create, retrieve, update, delete) operations.

What is an API?

An API (or Application Programming Interface) provides a method of interaction between two systems.

What is a RESTful API?

A RESTful API (or application program interface) uses HTTP requests to GET, PUT, POST, and DELETE data following the REST standards. This allows two pieces of software to communicate with each other. In essence, REST API is a set of remote calls using standard methods to return data in a specific format.

The systems that interact in this manner can be very different. Each app may use a unique programming language, operating system, database, etc. So, how do we create a system that can easily communicate and understand other apps?? This is where the Rest API is used as an interaction system.

When using a RESTful API, we should determine in advance what resources we want to expose to the outside world. Typically, the RESTful API service is implemented, keeping the following ideas in mind:

  • Format: There should be no restrictions on the data exchange format
  • Implementation: REST is based entirely on HTTP
  • Service Definition: Because REST is very flexible, API can be modified to ensure the application understands the request/response format.
  • The RESTful API focuses on resources and how efficiently you perform operations with it using HTTP.

The features of the REST API design style state:

  • Each entity must have a unique identifier.
  • Standard methods should be used to read and modify data.
  • It should provide support for different types of resources.
  • The interactions should be stateless.

For REST to fit this model, we must adhere to the following rules:

  • Client-Server Architecture: The interface is separate from the server-side data repository. This affords flexibility and the development of components independently of each other.
  • Detachment: The client connections are not stored on the server between requests.
  • Cacheability: It must be explicitly stated whether the client can store responses.
  • Multi-level: The API should work whether it interacts directly with a server or through an additional layer, like a load balancer.

#tutorials #api #application #application programming interface #crud #http #json #programming #protocols #representational state transfer #rest #rest api #rest api graphql #rest api json #rest api xml #restful #soap #xml #yaml