Notes

JavaScript miscellaneous notes

author: Paul Kim

categories: js

tags: js

JavaScript - miscellaneous notes


capitalize first letter of word using rest ... syntax

function capitalize([first, ...rest]) {
  return `${[first.toUpperCase(), ...rest].join('')}`
}

difference between Array.prototype.find() and Array.prototype.filter():

  • find returns the first value
  • filter returns all values

find the first word that start with 'btn-'

'btn -btn btn-'
  .split(/\s+/)
  .find((e) => e.startsWith('btn-'))(
    // "btn-"
    'btn -btn btn- btn -btn btn-'
  )
  .split(/\s+/)
  .find((e) => e.startsWith('btn-'))(
    // "btn-"

    'btn -btn btn-'
  )
  .split(/\s+/)
  .filter((e) => e.startsWith('btn-'))(
    // ["btn-"]
    'btn -btn btn- btn -btn btn-'
  )
  .split(/\s+/)
  .filter((e) => e.startsWith('btn-'))
// ["btn-", "btn-"]

convert object to query string (i.e., {foo: "bar", abc: 123} to abc=123&foo=bar)

const toQueryString = o => Object.keys(o) .sort() .map(k => ${k}=${o[k]}) .join('&')

const qs = toQueryString({foo: "bar", abc: 123}) // abc=123&foo=bar


MDN JavaScript Guide

Google Developers


ChromeEdgeFirefoxOpera

© 2021 paulkode.com. All rights reserved.