For-each over an array in JavaScript

TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly for (const element of theArray) { // …use `element`… } forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) theArray.forEach(element => { // …use `element`… }); a simple … Read more

What is the maximum length of a URL in different browsers?

Short answer – de facto limit of 2000 characters If you keep URLs under 2000 characters, they’ll work in virtually any combination of client and server software. If you are targeting particular browsers, see below for more details on specific limits. Longer answer – first, the standards… RFC 2616 (Hypertext Transfer Protocol HTTP/1.1) section 3.2.1 … Read more

Which equals operator (== vs ===) should be used in JavaScript comparisons?

The strict equality operator (===) behaves identically to the abstract equality operator (==) except no type conversion is done, and the types must be the same to be considered equal. Reference: Javascript Tutorial: Comparison Operators The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the … Read more

How do I execute a program or call a system command?

Use the subprocess module in the standard library: import subprocess subprocess.run([“ls”, “-l”]) The advantage of subprocess.run over os.system is that it is more flexible (you can get the stdout, stderr, the “real” status code, better error handling, etc…). Even the documentation for os.system recommends using subprocess instead: The subprocess module provides more powerful facilities for … Read more

How do I discard unstaged changes in Git?

For all unstaged files in current working directory use: git restore . For a specific file use: git restore path/to/file/to/revert That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff … Read more

What is the difference between “let” and “var”?

Scoping rules The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope). function run() { var foo = “Foo”; let bar = “Bar”; console.log(foo, bar); … Read more

How to disable text selection highlighting

UPDATE January, 2017: According to Can I use, the user-select is currently supported in all browsers except Internet Explorer 9 and its earlier versions (but sadly still needs a vendor prefix). These are all of the available correct CSS variations: .noselect { -webkit-touch-callout: none; /* iOS Safari */ -webkit-user-select: none; /* Safari */ -khtml-user-select: none; /* … Read more