JS Note
Template literals (String literals)
add variables at runtime
1 | let msg = `some ${variable1}, |
use tag function to add bold style to variables in template literals
1 | function highlighText(strings, ...values) { |
var, let and const
var:
- no block scope
- can be redeclared anywhere
- can be used and reassigned anywhere
let:
- block scope
- can’t be redeclared within scope
- can be reassigned within scope
const:
- block scope
- can’t be reassigned or redeclared
- the value can be changed
const is more used as readability purpose
1 | const arr = [3, 4, 5]; |
Destructing an array or object
array:
- don’t have to catch all values in the array
- variable is undefined if arr is not enough to unpack
- can set default value
1 | var [a, b = true, c, ...moreArgs] = arr; |
object:
1 | var { Id, ApplicantName = "Barry" } = obj; |
String
1 | str.trim(); // trim white space |
Number
1 | Number.isInteger(num); // 25.0 is integer |
Symbol
1 | var id = Symbol("My Id"); |