You can send data with the application/x-www-form-urlencoded content type (the one that encodes values similar to a query string, e.g. first=1&second=2) using a URLSearchParams object, like so:

fetch(url, {
  method: "POST",
  body: new URLSearchParams({first: 1, second: 2})
});
Read more

There's an API in Chrome and Firefox (that I know of) that lets you get localized strings with variable replacements from a JSON file that you provide.

var message = browser.i18n.getMessage("messageContent", target.url);
console.log(message);
Read more

Arrays can be copied using spread syntax, introduced in ES6. To illustrate:

let a = [1, 2, 3];

// This won't work.
let b = a;
b[0] = 99;
console.log(a[0], b[0]); // prints "99 99"

// But this will.
let c = [...a]; // before ES6 there was an even uglier way of doing this using "slice", but let's never talk about that
c[0] = 12345;
console.log(a[0], c[0]); // prints "99 12345"
Read more

There are different types of functions in modern-ish JavaScript: regular, async, and generator functions. However, typeof returns "function" for all of them. One way (the only?) to differentiate between them is by using toString.call(myFunc).

Read more

Due to a bug in the original C code for the implementation of Javascript, this awesome thing happens:

const a = null;
console.log(typeof a); // prints "object"
Read more

Here's yet another one of Javascript's endless oddities: the switch statement uses strict comparison (think === instead of ==).

Read more

There's a bunch of Web APIs that are pretty usable today:

  1. Detect page visibility
  2. Detect online state
  3. Vibration
  4. Detect orientation

...and more! clipboard, ambient light detection, battery status, etc.

Read more

You can run timers on the console object, like this:

console.time('a');
costlyOperation();
console.timeEnd('a'); // outputs how much time has passed since the call above
Read more

There's a nice utility that you can use to kill a process based on the port it's listening to, so for example if you have a server listening on port 80, you can run $ fkill :80 and it will murder it.

Only problem, it's made using Javascript, but if you're OK with having Node and plain-text JS files in your bin folder, you can install it with $ npm install --global fkill-cli.

Read more
Subscribe to Javascript