JavaScript¶
Strings¶
Template Literals¶
Feels like Python f"formatted_string"
myTag`A${1}B${2}C${3}D${4}E${5}F`;
function myTag(strings, ...args) {
console.log(strings); // ['A', 'B', 'C', 'D', 'E', 'F']
console.log(args); // [1,2,3,4,5]
return strings;
}
JS Regex¶
[[python-regex]]
The dot in RegExp doesn't match all characters in JS
RegExp.test()
-> boolean¶
String.match()
vs RegExp.exec()
¶
regex - match Vs exec in JavaScript
match
can do all at once with global flag
without global flag, will find the first
RegExp.exec()
¶
has a nice syntax for looping with capturing groups?
One at a time
// NEED THE global flag
myRegexp = /\[(\d+)\]/g
while (result = myRegexp.exec("[22].[44].[33].")) {
console.log(result, myRegexp.lastIndex);
// [
// '[33]',
// '33',
// index: 10,
// input: '[22].[44].[33].',
// groups: undefined
// ] 14
}
Numbers¶
NaN¶
Functions¶
kwargs (obj --> params)¶
How do I pass this in args = {a: 1, "1": 2}
?
Can't destructure
Solution: get the object values as an array¶
// order: [Does JavaScript guarantee object property order?](https://stackoverflow.com/a/23202095)
// 1. integer keys (like "11")
// 2. string keys
// 3. Symbol keys
argValues = Object.values(args); // [2,1]
c = sum(...argValues);
console.log(c);
Importing¶
CommonJS vs AMD
at the start, commonJS sucked on browsers
AMD was used because people had to use it
you explicitly declare your dependencies for each function/module
now, neither are the best
Use a build tool
Import a package in the dev tools¶
Symbols¶
Why symbols¶
- All symbols are unique
- For fully unique keys
- private properties
- (PITA to access)
Unique sentinel values, identity checks, and when to use object() instead of None
React uses symbols
Last update:
2023-04-24