Math.random().toString(36).substr(2);
This simply generates a random float, casts it into a String using base 36 and remove the 2 first chars 0 and. Note that this is not a replacement for UUID generation.
var newA = myArray.slice(0);
This will return a copy of the array, ensuring no other variables point to it.
"<b>A</b>".replace(/<[^>]+>/gi, "");
This is using a simple regular expression to remove any string that looks like <xxx>
where x can be any char, including /
.
function foo(opts) {
var options = opts || {};
}
You will see this in any decent JS code.
If opts
is defined and not “binarily” false it will be assigned to options
, otherwise it will assign an empty dictionary {}
.
var str = "Pouet this string.";
str.split('').reverse().join('');
// Output: ".gnirts siht teuoP"
// Keep words order with:
str.split(' ').reverse().join(' ');
// Output: "string. this Pouet"
// The first example splits on every character, reverses them and puts them back together.
The second splits only on words and joins them back separated by a space.
Hi @alphashuro, thanks for your comment! I'm more of a Python developer indeed.
To be pedantic,
{}
is an object in both languages, JavaScript and Python. And while we often use it to define "classes" in JavaScript, we also quite often use it just as a dictionary, for instance as in the example above, to store options.These one-liners are meant to be used both in node and in browsers, where the support for ES6 isn't there 100% just yet. That being said, as the support is quite good already I may indeed include ES6 examples is the very near future.