How to format a JavaScript date

For custom-delimited date formats, you have to pull out the date (or time) components from a DateTimeFormat object (which is part of the ECMAScript Internationalization API), and then manually create a string with the delimiters you want. To do this, you can use DateTimeFormat#formatToParts. You could destructure the array, but that is not ideal, as … Read more

How do I check if an array includes a value in JavaScript?

Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE:  Run code snippet You can also use Array#indexOf, which is less direct, but doesn’t require polyfills for outdated browsers.  Run code snippet Many frameworks also offer similar methods: jQuery: $.inArray(value, array, [fromIndex]) Underscore.js: _.contains(array, value) (also aliased as _.include and _.includes) Dojo Toolkit: dojo.indexOf(array, value, [fromIndex, findLast]) Prototype: array.indexOf(value) MooTools: array.indexOf(value) MochiKit: findValue(array, value) MS … Read more

“Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6

I’m using ArcGIS JSAPI 4.12 and wish to use Spatial Illusions to draw military symbols on a map. When I add milsymbol.js to the script, the console returns error Uncaught SyntaxError: Cannot use import statement outside a module` so I add type=”module” to the script, and then it returns Uncaught ReferenceError: ms is not defined … Read more

What is the JavaScript version of sleep()?

2017 — 2021 update Since 2009 when this question was asked, JavaScript has evolved significantly. All other answers are now obsolete or overly complicated. Here is the current best practice:  Run code snippet This is it. await sleep(<duration>). Or as a one-liner: Note that, await can only be executed in functions prefixed with the async keyword, or at the … Read more

addEventListener vs onclick

Both are correct, but none of them are “best” per se, and there may be a reason the developer chose to use both approaches. Event Listeners (addEventListener and IE’s attachEvent) Earlier versions of Internet Explorer implement javascript differently from pretty much every other browser. With versions less than 9, you use the attachEvent[doc] method, like … Read more

ES6 Map in Typescript

EDIT (Jun 5 2019): While the idea that “TypeScript supports Map natively” is still true, since version 2.1 TypeScript supports something called Record. Unfortunately the first generic parameter (key type) is still not fully respected: even with a string type, something like peopleA[0] (a number) is still valid. EDIT (Apr 25 2016): The answer below is old and should not be considered the best answer. … Read more

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş