Skip to content

Latest commit

 

History

History
81 lines (57 loc) · 1.52 KB

JavaScript.md

File metadata and controls

81 lines (57 loc) · 1.52 KB

Javascript

Get values from an object

Object.values()

(does not have wide browser support yet)

Get keys from an object

Object.keys()

check the key presence in an object:

const a = { key: 1 }
'not-present' in a // false
'key' in a // true

best conditional assignment:

;(function (smth) {
  if ( 'key' in smth ) return;

  smth.key = {
    // do your stuff
  }
})('smth' in window && window.smth || (window.smth = {}))

Binary to decimal:

parseInt('011',2)

Decimal to binary

(4).toString(2)

Get char code:

"a".charCodeAt(0) // 97

Get char by code:

String.fromCharCode(97); // 'a'

extend console.log

source

log = function() {
    const context = "My Descriptive Logger Prefix:";
    return Function.prototype.bind.call(console.log, console, context);
}() // <- !!!

Semicolons:

do not use them semicolons mandatory:

;(d + e).print() // line starts with parenthesis or square bracket

quote from the article:

If you choose to omit semicolons where possible, my advice is to insert them immediately before the opening parenthesis or square bracket in any statement that begins with one of those tokens, or any which begins with one of the arithmetic operator tokens /+, or - if you should happen to write such a statement.