Javascript idioms for doing ordinary things

I have been programming JS for several years and am still finding new shortcuts to work with. I am wondering if there are others that I do not know about.

Here are the shortcuts that I know of:

edit . I agree that you never do this at all, and that there may be a better way to describe it as less broad, but the best I can do is to describe an example of it.


Instead of this

if("foobar".indexOf("foo") > -1) 

Do it

 if(~"foobar".indexOf("foo")) 

Instead of this

 var foo = Math.floor(2.333) 

Do it

 var foo = ~~2.333 

Instead of this

 var foo = parseFloat("12.4") var bar = parseInt("12", 10) 

Do it (not a huge fan of it)

 var foo = +"12.4" var bar = +"12" 

Instead of this

 if(isNaN(foo) 

Do it

 if(foo != foo) 

Instead of this

 (function(){ ... ])() 

Do it

 !function(){ ... }() 

Convert anything to a boolean value prefixed with !!

 var isFoo = !!foo 

You have this, my list of things that never do with your colleagues.

Can I add anything else?

+7
javascript
source share
3 answers

This question is likely to be closed as too broad, and this answer may collect its fair share of downvotes for not responding directly, but here it goes.

Please, please , be extremely careful in using shortcuts in a programming language, because ... really ... who do they help?

Most of these shortcuts sacrifice clarity and evidence of keystrokes. You will not find a single competent professional coder who agrees that this is a reasonable compromise.

Consider

 if("foobar".indexOf("foo") > -1) 

to

 if(~"foobar".indexOf("foo")) 

You have saved 4 characters ... whoopie! However, you also guaranteed that anyone who does not know this shortcut has a very subtle chance to find out what is happening here ... and, of course, not with ease.

Reading the definition of indexOf enough to understand the explicit version. For the second, you need to understand what ~ means (which is a rather unusual statement in JS). Then you need to know what a bitwise complement of -1 is. Then you need to understand that this is true.

This is a stupid compromise, and it is a sign of many of these idioms.

Please do not do this. This is not the 80s.

+11
source share
  • n | 0 n | 0 floor n (only if n is within the 32-bit integer range). This is faster than Math.floor() in most browsers that I last checked.
  • undefined == null , but none of them are false .
  • Instead of x == 'a' || x == 'b' || x == 'c' x == 'a' || x == 'b' || x == 'c' x == 'a' || x == 'b' || x == 'c' you can do ['a', 'b', 'c'].indexOf(x) !== -1

The only really short shortcut I've seen in production code is unary + to convert strings to numbers.

+2
source share

I see this a lot:

 myString = "Some string." b = myString[myString.length - 1]; // get the period 

Must be

 b = myString.slice(-1); 

For while loop.

 for (var i=0; i < a.length; i++) { var x = a[i]; console.log(x); } 

to

 for (var i=0,x; (x=a[i++]) != null; ) { console.log(x); } 

Put this at the top of the script.

 String.prototype.has = Array.prototype.has = function(a){ return this.indexOf(a) !== -1; } 

Includes these

 if (myString.indexOf(a) !== -1) if (myArray.indexOf(a) !== -1) 

to these

 if (myString.has(a)) if (myArray.has(a)) 

If you need to put 42 in your code, but secretly.

 var answer = ((((1<<2)|1)<<2)|1)<<1; 

CoffeeScript

... need to say more?

+2
source share

All Articles