This post is the memo from Eloquent JavaScript Chapters 1 through 11.

1 Compute and convert to a string.

`half of 100 is ${100 / 2}`

2 The difference from var and let. Var is global while let is local.

3 Exception

// throw one error
throw new Error("Invalid direction: " + result);

try {
    // test something
} catch (error) {
    // error is the error that is caught
} finally {
    // no matter what happens, run this code after trying to run the code in the try block
}

4 Regular Expression

// new
let re1 = new RegExp("abc");
let re2 = /abc/;

// pattern match
console.log(/abc/.test("abcde"));
// → true
console.log(/abc/.test("abxde"));
// → false

// character set
console.log(/[0123456789]/.test("in 1992"));
// → true
console.log(/[0-9]/.test("in 1992"));
// → true

// ? means optional
let neighbor = /neighbou?r/;
console.log(neighbor.test("neighbour"));
// → true
console.log(neighbor.test("neighbor"));
// → true

// * or + means more than one element at a time
let cartoonCrying = /boo+(hoo+)+/i;
console.log(cartoonCrying.test("Boohoooohoohooo"));
// → true

//get the detail pattern match information
let match = /\d+/.exec("one two 100");
console.log(match);
// → ["100"]
console.log(match.index);
// → 8

Leave a Reply

Your email address will not be published. Required fields are marked *