Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
5 handy Javascript one-liners — First published on fullweb.io issue #48

5 handy JavaScript one-liners

1. Generate a random string:

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.

2. Clone an array:

var newA = myArray.slice(0); 

This will return a copy of the array, ensuring no other variables point to it.

3. Remove HTML tags:

"<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 /.

4. Set a default value:

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 {}.

5. Reverse a string:

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.

@alphashuro
Copy link

alphashuro commented May 17, 2016

are you from python? {} is not a dictionary, its an object. Also, you should probably add the equivalent es6 versions of your on liners because node now supports most features of es6.

@nepsilon
Copy link
Author

nepsilon commented May 17, 2016

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.

@joeoravec
Copy link

joeoravec commented May 17, 2016

Don't forget the period included in your initial str value of "Pouet this string." in #5. Your outputs would be ".gnirts siht teuoP" and "string. this Pouet" instead.

@nepsilon
Copy link
Author

nepsilon commented May 18, 2016

Hey @joeoravec, thanks for the feedback! I just updated the Gist.

@alphashuro
Copy link

alphashuro commented May 18, 2016

In JavaScript there actually aren't any "classes", {} is actually a real object because at any point you can define functions on a {} that would have a reference to the calling object, were as dictionaries exclusively store key/value pairs. If you have seen the word "class" anywhere its actually just syntatic sugar to hide the prototypal inheritence that happens in the background.

@nepsilon
Copy link
Author

nepsilon commented May 18, 2016

Yes, I have put the word classes between quotes to mean that there are no classes per se in JavaScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment