DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Code_Regina
Code_Regina

Posted on

|JavaScript| JavaScript: Newer Features

          -Default Params
          -Spread in Function Calls
          -Spread with Array Literals
          -Spread with Objects
          -Rest Params 
Enter fullscreen mode Exit fullscreen mode

Default Params


function multiply(a, b = 1) {
 return a * b; 
}

multiply(4); 
multiply(4, 5); 

Enter fullscreen mode Exit fullscreen mode

In the code, a has no default value, therefore, a will be whatever you pass in as the first argument.
Meanwhile, b does have a default value. If you leave it off, it will default to one.

Spread in Function Calls

Spread syntax allows an iterable such as an array 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.

spread is used for function calls.
Expands an iterable array, string into a list of arguments.


const nums = [ 9, 3, 2, 8 ];
Math.max(nums); 
Math.max(...nums); 

Enter fullscreen mode Exit fullscreen mode

Spread with Array Literals

Create a new array using an existing array. Spreads the elements from one array into a new array.


const nums1 = [ 1, 2, 3 ]; 
const nums2 = 4, 5, 6 ]; 

[ ...nums1, ...nums2 ]; 

[ 'a', 'b', ...nums2 ];

[ ...nums1, ...nums2, 7, 8, 9 ];

Enter fullscreen mode Exit fullscreen mode

Spread with Objects

Copies properties from one object into another object literal.


const feline = { legs: 4, family: 'Felidae' };
const canine = { family: 'Caninae', furry: true }; 

const dog = { ...canine, isPet: true }; 

const lion = { ...feline, genus: 'Panthera' };

const catDog = { ...feline, ...canine }; 

Enter fullscreen mode Exit fullscreen mode

We can spread properties from an object into a new object.

Rest Params

The arguments object is available inside every function.
Its an array-like object that has a length property but does not have array methods like push or pop.
Contains all the arguments passed to the function. Not available inside of arrow functions.



function sumAll() {
 let total = 0; 
 for (let i = 0; i < arguments.length; i++)
{ total += arguments[i];
}
return total; 

}

sumAll(8, 4, 3, 2); 
sumAll(2, 3); 

Enter fullscreen mode Exit fullscreen mode

Its called an array like object because we can use the indices to access elements outs. It works exactly like an array, except we don't have to access array methods like push or pop.

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!