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.
//Rest collect the array values as a variable
let [uno, ...due] = [1,2,2,3,4,5,6,8]
console.log( due ) //[2, 2, 3, 4, 5, 6, 8]
//Spread expands an army into the element
const oneToFive = [1, 2, 3, 4, 5];
const oneToTen = [...oneToFive, 6, 7, 8]; //[1, 2, 3, 4, 5, 6, 7, 8]
We can spread the rest variables in any array index position.
let deck1 = [1,500,1,2,3,55,6,5,4,6]
const [uno, due, ...rest] = deck
console.log( [due, ...rest, uno] ) //[500, 1, 2, 3, 55, 6, 5, 4, 6, 1]
//Without the ...spread it would return an array
console.log( [due, rest, uno] ) //[500, [1, 2, 3, 55, 6, 5, 4, 6] , 1]
We can rest a parameter and use empty commas to skip array elements:
let deck = [ 100, 23, 45, 12, 34 ]
function skip( [, , ...rest] ){
return [..rest]
}
skip( deck ) //[45, 12, 34]
It can also work as a concat() and split() method for Math operations.
//Spread as concat() will render using the variables order
function combineArrays(arr1, arr2, arr3) {
return [...arr3 , ...arr2, ...arr1];
}
combineArrays( [12, 34], [23, 45], [100] ) //[ 100, 23, 45, 12, 34 ]
//We can use Spread on strings
let siamo = "siamo"
[...siamo] //['s', 'i', 'a', 'm', 'o']
siamo.split("") //['s', 'i', 'a', 'm', 'o']
//On Math we don't need loops/filters
let mat = [1, 25, 50, 100]
let maximus = Math.max( ...mat ) //100
Which is also the better way to clone:
let newDeck = [...deck];
We can parse variables on function return:
//We can take a function returns values
const f = () => { [12, 13] }
const [a,b]= f()
console.log(a, b) //12, 13
And deconstruct functions' arrays/objects returns.
function guiss(q,w){
let tron = q+w
let bron = q-w
return [tron, bron]
}
let [gui, pui] = guiss(5,1) //the returns will be in array order
console.log( gui, pui ) //6, 4
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.
//new Error being its constructor
let errore= "Error number " + Math.random()
const blokka= new Error(errore) //the argument being the error.message
console.log(blokka)
console.log(blokka.message)
throw blokka //throw new Error(errore)
There are Built-in constructor errors SyntaxError, ReferenceError, and TypeError:
//their object.name is included
let error = new ReferenceError("this is the argument");
console.log(error.name); // ReferenceError
console.log(error.message); // this is the argument
To Error handling, we TRY (the throw error) and CATCH (a response) without breaking the code:
//Both need to share the same space (like in a function)
try{
throw new Error("printed after try")
}catch(error){
console.log(error.message) //printed after try
}
TRY can also catch standard code errors:
try {
let jj= { element }
let user = JSON.parse(jj);
} catch (err) {
console.log( err ); //ReferenceError for element not being defined
}
The finally{} code gets executed right before an error is catch:
//finally{} can trigger without a catch{}
try {
alert( 'try' );
if (confirm('Make an error?')) BAD_CODE();
} catch (err) {
alert( 'catch' );
} finally {
alert( 'finally' );
}
Last updated
Was this helpful?