Tuesday, April 27, 2021

Javascript Tips and Tricks

  • It is not possible to reassign an argument in a method. (a) => { a = 2; } will not work.
    However, it is possible to reassign a property of an argument object in a method, so (a) => { a.value = 2; } will work.
  • Promise execution has a different queue than normal code. This can be important in testing async code: in the test we'll have to make sure the test execution waits for the promise to resolve, before testing the return value or side effect.
  • Optional chaining saves you the trouble of testing for undefined keys in nested objects. Eg. person.address?.street?.name 
  • Use a variable's value as a key in and object with this syntax: { [yourKeyVariable]: someValue } 
  • ...

No comments:

Post a Comment