2

(Edit: I found this syntax while reading someone else's code)

Is there any good reason for using the following syntax for setting a variable in javascript:

this.index >= this.items.length && (this.index = 0);

Is it just to get the expression on one line of code, or is there another, dare I say better, reason for this way of doing it...

Edit: The code is equivalent to:

if (this.index >= this.items.length) { 
    this.index = 0; 
}
6
  • 1
    This looks like an odd way of writing if( this.index >= this.items.length ) { this.index = 0; } Commented Sep 25, 2013 at 12:25
  • 1
    I keep seeing more and more people asking about this... I don't know who is telling developers to use this crazy style of writing JS, but it's definitely something that's really hard to read when you're trying to understand what some code is doing and I would never recommend using this. Commented Sep 25, 2013 at 12:27
  • 1
    Your edit is more readable in my opinion. :) Commented Sep 25, 2013 at 12:28
  • I found this bit of code while reading someone else's code, and I had to read it a couple of times to understand what it does. I thought perhaps there was a good reason for doing it this way. Btw: The code works, it's just a very odd way of doing it... Commented Sep 25, 2013 at 12:29
  • "and I had to read it a couple of times to understand what it does" this is why you don't write code like this. Commented Sep 25, 2013 at 12:29

3 Answers 3

3

Wow, no. Just no. I consider myself a pretty good JavaScript programmer and I can't tell you what this code does.

Does it just do this.index = this.items.length or does it do something weird like this.index = false?

If you intend:

if (this.index >= this.items.length) {
    this.index = 0;
}

Then you could consider:

this.index = (this.index >= this.items.length) ? 0 : this.index;

But that doesn't really improve it, does it?

Sign up to request clarification or add additional context in comments.

2 Comments

if (this.index >= this.items.length) { this.index = 0; }
Well, if the first statement is true, then the second statement will run, it's equivalent to if( this.index >= this.items.length ) { this.index = 0; }
1

No, there isn't a good reason, i think people just do it a lot so it's a "one liner", you're better off using a if like this.

if( this.index >= this.items.length ) {
    this.index = 0;
}

Because as you can see people get confused with what it actually does.

But you can still keep it a one liner like so

if( this.index >= this.items.length ) { this.index = 0; }

2 Comments

if without brackets is prone to bugs because it may react to an else statement one the next line. I typically find JSLint (or equivalent) to be extremely helpful in ensuring a good, silly-bug free, coding standard.
Then you should turn on some knobs and make it more strict because this is absolutely not acceptable. Let the minifier / compiler do the fancy optimization.
1

I think the intention of this code is

 this.index = this.index >= this.items.length ? this.items.length : 0;

or

 this.index = this.index >= this.items.length ? 0 : this.items.length;

3rd possible intention (that might be really what it should do).

 this.index = this.index >= this.items.length ? 0 : this.index;

But that's just best guess, the line doesn't make any sense at all... And you usually do not assign a variable like this. Actually it doesn't even assign the variable this.index.

Actually your code is really working. See working example here: http://jsfiddle.net/Elak/EthsP/

Never saw this syntax before tbh and I write lots of JavaScript... But you never stop learning new stuff :p

3 Comments

Yes, your edit is right, you should know which one it's equivalent to
No. this.index is never assigned to this.items.length.
@AndréDion ok 3rd edit, that's it right? ^^ your code has actually a very strange syntax ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.