Starting from:

$30

AE502-Homework 1 Solved

what is var in javascript

var is the keyword that tells JavaScript you're declaring a variable. x is the name of that variable. = is the operator that tells JavaScript a value is coming up next.

 

what is ellips in javascript

Summary. In Javascript, ellipses ( … ) are used for two separate shorthands — rest syntax and spread syntax. Rest syntax will set the remaining elements of a collection to a defined variable.

 

Enter Javascript Ellipses (…)

Modern JavaScript allows us to use three dots, …, also known as an ellipsis, to perform certain transformations and shorthand methods. The use of ellipses falls into two categories: rest syntax and spread syntax. I’ll explain each of these in turn.

 

 

 

Rest Syntax

Rest syntax allows a programmer to retrieve the remaining items — the rest of the items — in a collection that the programmer has not used, such as objects, arrays, or function arguments.

Let’s take a look at a few examples.

 

Rest Syntax

Rest syntax allows a programmer to retrieve the remaining items — the rest of the items — in a collection that the programmer has not used, such as objects, arrays, or function arguments.

Let’s take a look at a few examples.

Rest syntax with object deconstruction

Modern JavaScript allows us to deconstruct objects. That is to say, we can define variables and assign values to them quickly from an existing object. Here’s a simple example:

In this example, we first define an object. Then, using object deconstruction, we pull the define variables c and d and assign them the values respective to their names from our object. Variables c and d can then be used, as we see in the log lines following the deconstruction.

With “rest” syntax, we can get the remaining items in the object that were not explicitly deconstructed, as in the example below:

As you can see, after deconstructing c and d from obj, we deconstruct …restOfObj, which is assigned the value of a new object with all the keys from obj, except those that were explicitly used. It’s important to note that rest syntax must be used at the end of a deconstruction and is indicated by a preceding variable name, in this case restOfObj, with an ellipsis (…).

Rest syntax with array deconstruction

Modern JavaScript also allows us to deconstruct arrays. Here’s a simple example of this:

Here, we first define an array and assign it to variable arr. We then deconstruct the array to pull out the first three items and assign them to variables firstItem, secondItem, and thirdItem. As you can see, the first value of the array is assigned to the first variable defined in the deconstruction, the second to the second, and so on.

With rest syntax, we can pull out the first few items in an array, then assign the remaining items to a new array:

As you can see, by using rest syntax, we’ve created a variable called restOfArr and assigned it a new array with the remaining items from arr that were not explicitly deconstructed. As with rest syntax with object deconstruction, rest syntax for arrays must be used at the end of a deconstruction. The end is indicated by the preceding variable name, in this case restOfArr with an ellipsis (…).

Rest syntax with function parameters

Function definitions also accept rest syntax for defined parameters, as shown:

As you can see, we’ve defined function parameters a, b, and c. Following these, we use an ellipsis with a parameter name, …restOfProvidedParameters, to assign the remaining provided parameters and populate them in an array. Since the first, second, and third parameters are explicitly used in the function definition as a, b, and c, they’re not included in the restOfProvidedParameters array.

Rest syntax gotchas

Given the name, rest syntax cannot be used to get the beginning elements of an object, array, or parameter definitions. For example, this is not possible:

function (...firstParametersExceptTheLast, lastParameter) {}// SyntaxError: Rest parameter must be last formal parameter

Similarly, it cannot be in the middle elements, like this:

function (firstParameter, ...middleParameters, lastParameter) {}// SyntaxError: Rest parameter must be last formal parameter

Spread Syntax

Spread syntax allows a simplified way for a collection, like an object or array, to interact with other collections or functions. Let’s take a look at the different combinations.

Spread syntax with arrays to function invocations

Here we define a function and an array of length 2. We then invoke the function with a string a, the spread array ...arr, and string d:

When fun is invoked string a is provided first to aParam. Next, arr is spread. There are two items in the array so arr, b, c, bParam and cParam will get those values. When the spread is complete the remaining string value d is provided to the dParam of the function.

Spread syntax with arrays to other arrays

Spread syntax can also be used to combine arrays:

const arr1 = [ 'b', 'c' ];
const arr2 = [ 'e', 'f' ];const combinedArr = [ 'a', ...arr1, 'd', ...arr2, 'g' ];console.log(combinedArr); // [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ];

Here we see two arrays, arr1 and arr2 defined. We create a new array combinedArr and in its creation, we “spread” arr1 and arr2 within it. The result is that the new array, combinedArr, contains the values of the spread arrays.

Spread syntax with objects to other objects

Spread syntax can be used to add and override key values in an object. The rule is that the last provided value for a key takes precedence. Here’s an example:

As you can see, we’ve defined three objects: obj1, obj2, and obj3. We then defined a new object, combinedObj, and spread the previously defined three objects within it. The last provided value for a given key overrides previously provided values. Here are some explanations:

Key ais 1 because, although it is explicitly defined as 0 in combinedObj, obj1 is spread after the explicit definition and overrides it.
Key dis 0 because, although obj2 is spread and defines it as 2, it is subsequently explicitly set to 0 within combinedObj.
Key fis 0 because, although obj1, obj2, obj3 are spread and define and override the key respectfully as 1, 2, and 3, it’s finally explicitly set to 0 from within combinedObj.
Spread Syntax Extras

In JavaScript, an array is also an object, so it can also be spread to an object:

const arr = [ 'a', 'b', 'c' ];const obj = { ...arr };console.log(obj);
// { 0: 'a', 1: 'b', 2: 'c' }

Spreading an array to an array can be used to make a shallow copy of the array:

const originalArr = [ 'a', 'b', 'c' ];const arrayShallowCopy = [ ...originalArr ];arrayShallowCopy[1] = 'overrideShallowCopyValueOnly';console.log(originalArr);
// [ 'a', 'b', 'c' ]
// (The value is unchanged)console.log(arrayShallowCopy);
// [ 'a', 'overrideShallowCopyValueOnly', 'c' ]

Spread syntax gotchas

Spread syntax does not work with all combinations of collections to arrays, objects, and functions. Here are some that do not work:

Objects cannot spread to function invocations
Objects cannot spread to arrays
Summary

In Javascript, ellipses (…) are used for two separate shorthands — rest syntax and spread syntax. Rest syntax will set the remaining elements of a collection to a defined variable. Spread syntax allows the elements of a collection to interact with other objects, arrays, and functions in certain ways — normally to provide or override values.

 

More products