Blog Hoch
Jun 2018·1 tup leghDe'wI' ghItlhta'

Javascript Snippets

#javascript#snippets

naDev JavaScript snippets vIqoj. tlhegh 'oH 'ej poH Dung law' vIchel :)

Array Daq Dochvam Doch random? nIv pagh!

var items = [123, 'John', 11111, 'Doe', 'foo', 2924, 'bar', 'foobar', 55, 'baz'];
var randomItem = items[Math.floor(Math.random() * items.length)];

min 'ej max ghaj random mI'? yIlaD!

var x = Math.floor(Math.random() * (max - min + 1)) + min;

"SO" tu'bogh: Javascript shorthand if/else. 'e' vISovbe'. Hol HoS law'... :D

var y = (x == 2 ? "yes" : "no");

Source: stackoverflow.com/questions/11069278

Optional parameters

if (typeof optionalArg === 'undefined') {
  optionalArg = 'default';
}

Promise

new Promise(function(resolve, reject) {
  alert("Aktion 1");
  resolve();
}).then(function() {
  alert("Aktion 1 beendet");
});
chu'ScrollblurqanBash Snippets