throwstatement will stop execution of the script, unless you handle it
throw new Error('something went wrong')const myError = new Error('This is the error message');
console.log(myError.name); // Error
console.log(myError.message); // This is the error messageErrorinstances have name and message properties.
// this:
const x = Error('I was created using a function call!');
// has the same functionality as this:
const y = new Error('I was constructed via the "new" keyword!');- When
Erroris used like a function -- withoutnew, it will return anErrorobject. Therefore, a mere call toErrorwill produce the same output that constructing anErrorobject via thenewkeyword would
function f() {
try {
console.log(0);
throw 'bogus';
} catch(e) {
console.log(1);
return true; // this return statement is suspended
// until finally block has completed
console.log(2); // not reachable
} finally {
console.log(3);
return false; // overwrites the previous "return"
console.log(4); // not reachable
}
// "return false" is executed now
console.log(5); // not reachable
}
console.log(f()); // 0, 1, 3, false- When logging errors to the console inside a
catchblock, usingconsole.error()rather thanconsole.log()is advised for debugging as it formats the message as an error and adds it to the list of error messages generated by the page - The
finallyblock executes whether or not an exception is thrown. - If the
finallyblock returns a value, this value becomes the return value of the entiretry-catch-finallyproduction, regardless of any return statements in thetryandcatchblocks - Overwriting of return values by the
finallyblock also applies to exceptions thrown or re-thrown inside of thecatchblock
const currentYear = new Date().getFullYear();
let car = {
yearPurchased : 2001,
get yearsOwned() {
return currentYear - this.yearPurchased;
},
set yearsOwned(years) {
this.yearPurchased = currentYear - years;
}
}
console.log(car.yearPurchased);
console.log(car.yearsOwned);
car.yearsOwned = 5;
console.log(car.yearPurchased);let person = {
firstName : "Randy",
lastName: "Beltran",
get fullName() {
return `${this.firstName} ${this.lastName}` // String Literal
},
set fullName(name) {
[this.firstName, this.lastName] = name.split(' '); // Destructuring Assignment
}
}
console.log(person.fullName);
person.fullName = "Jason Humphrey";
console.log(person.firstName);
console.log(person.lastName);
// Use of Promises
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Data was returned');
}, 3000);
});
myPromise
.then(data => {
console.log(data);
});console.log('Start!');
const myPromise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('Data was returned');
}, 3000);
});
myPromise
.then(function(data) {
console.log(data);
})
console.log('End!');function closureStart() {
var topic = 'closure';
function display() {
alert(topic);
}
return display;
}
var myClosure = closureStart();
myClosure();let myString = 'Hello';
let myString2 = "Hello World";
let myString3 = `Hi ${myString}`;
let myNum = 4;
let isGood = true;
let myShoppingList = ['Flour', 'Sugar', 'Eggs'];
let myShoppingList2 = {
0: 'Flour',
1: 'Sugar',
2: 'Eggs'
};
let myInformation = {
street: 'Main St',
phone: '(555) 555-5555',
state: 'CA'
};
let myPoints = [
{
x: 1,
y:1
},
];
let myPoints2 = [
[1,1]
];
let myFriendsList = [
{
name: 'Sally',
list: [{
product: 'Cake',
brand: 'Betty Crocker'
}]
}
];
let storeCustomers = {
Walmart: myFriendsList
};
let scores = {
2009: [89, 79]
};
let scores2 = [[89, 79], [88, 78]];