JS 4, spread and rest operators, Error constructor and object, TRY, CATCH and FINALLY

We can use ES6 Syntax for default parameter in a function:

function defaultParameter(name = "sam") {
  return console.log( name);
}

defaultParameter("oltre")    //"oltre"
defaultParameter()           //"sam"

The rest/spread operator is a syntax that creates a copy of an iterable object, it can unpack and edit existing objects.

Its syntax doesn't mutate the iterable, it's not a function or a loop and we can't access the iteration process.

//The longer the array is, the more complex the copy operation becomes
//The push() method keeps its complexity indipendently of the length
//The push() will print the new length of the mutated array.

let base = [1, 2, 3, 4, 5]	
let base1 = [...base, 12]	
console.log( base1 )   //(6)[1, 2, 3, 4, 5, 12]
console.log( base )    //(5)[1, 2, 3, 4, 5]

let mosso = [1, 2, 3, 4, 5]
console.log( mosso.push(13) )	//6
console.log( mosso )		//(6)[1, 2, 3, 4, 5, 13]

The Rest operator can store only at the end of its array.

We can spread the rest variables in any array index position.

We can rest a parameter and use empty commas to skip array elements:

It can also work as a concat() and split() method for Math operations.

Which is also the better way to clone:

We can parse variables on function return:

And deconstruct functions' arrays/objects returns.

Error constructor and Object

Error is a javascript object, with .message and .name being its main property, and when throw it will stop the code.

we get the error printed and its .message property

There are Built-in constructor errors SyntaxError, ReferenceError, and TypeError:

To Error handling, we TRY (the throw error) and CATCH (a response) without breaking the code:

TRY can also catch standard code errors:

chevron-rightFiltering and reThrowing Errorshashtag

We can put TRY conditions to trigger and catch a new Error():

We filter the error.name with instanceof on catch{}

In this Fibonacci example we use outside variables and Date():

The finally{} code gets executed right before an error is catch:

Last updated